# Returning STRUCT from .api

**URL:** <https://forum.legato.io/t/returning-struct-from-api/6644>\
**Category:** Legato Application Framework\
**Created:** [November 24, 2023, 6:22am UTC](https://forum.legato.io/t/returning-struct-from-api/6644 "2023-11-24T06:22:22Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![french.bulldog](https://avatars.discourse-cdn.com/v4/letter/f/8c91f0/32.png) [@french.bulldog](https://forum.legato.io/u/french.bulldog)\
**Post date:** [November 24, 2023, 6:22am UTC](https://forum.legato.io/t/returning-struct-from-api/6644/1 "2023-11-24T06:22:22Z")

</div>

I’m having trouble returning a user defined STRUCT through a function in my .api file.

```auto
/home/f/legatoapps/_build_app/wp76xx/api/732e28177b4a96521bsb9d6c80fecb29/server/ctrlSerial_server.c:349:61: error: incompatible type for argument 2 of ‘ctrlPower_PackTestStruc’
     LE_ASSERT(ctrlPower_PackTestStruct( &_msgBufPtr, _result ));
                                                             ^
/home/d/Desktop/myLeafWorkspace/leaf-data/wp76xxdev_R16.3_L19.11.6/wp76-legato/framework/include/le_log.h:915:13: note: in definition of macro ‘LE_FATAL_IF’
         if (condition) { LE_FATAL(formatString, ## __VA_ARGS__ ) }
             ^ ~~~~~~~~
/home/f/legatoapps/_build_app/wp76xx/api/732e28177b4a96521bsb9d6c80fecb29/server/ctrlSerial_server.c:349:5: note: in expansion of macro ‘LE_ASSERT’
     LE_ASSERT(ctrlPower_PackTestStruct( &_msgBufPtr, _result ));
     ^ ~~~~~~~~
In file included from /home/f/legatoapps/_build_app/wp76xx/api/732e28177b4a96521bsb9d6c80fecb29/server/ctrlSerial_server.c:13:0:
/home/f/legatoapps/_build_app/wp76xx/api/732e28177b4a96521bsb9d6c80fecb29/ctrlSerial_messages.h:430:20: note: expected ‘const ctrlPower_TestStruc_t * {aka const struct <anonymous> *}’ but argument is of type ‘ctrlPower_TestStruc_t {aka struct <anonymous>}’
 static inline bool ctrlPower_PackTestStruct
                    ^ ~~~~~~~~~~~~~~~~~~~~~~~

```

These are my compilations errors, do you know how to fix this?  
My function in the .api is declared like:

```auto
FUNCTION TestStruct SendAndReceiveStruct
(
      string cmd[20] IN,
      string arg[20] IN
);

```

My struct in the .api is declared like:

```auto
STRUCT TestStruct 
(
    le_result_t res;
    int8 output;
);

```

My defined function in my main .c file is

```auto
ctrlPower_TestStruct_t SendAndReceiveStruct
(
      string cmd[20] IN,
      string arg[20] IN
);

```

My struct in the .api is declared like:

```auto
ctrlPower_TestStruct SendAndReceiveStruct(
    const char *cmd, // IN
	const char *arg // IN
)
{
     // process here 
     return (ctrlPower_TestStruct){.res = LE_OK, output = 1}; // just testing values
}

```

Thanks for your help

---

<div class="post-metadata">

**Author:** ![jyijyi](https://sea1.discourse-cdn.com/flex019/user_avatar/forum.legato.io/jyijyi/32/822_2.png) [@jyijyi](https://forum.legato.io/u/jyijyi)\
**Post date:** [November 24, 2023, 6:25am UTC](https://forum.legato.io/t/returning-struct-from-api/6644/2 "2023-11-24T06:25:54Z")

</div>

I doubt if you can return a STRUCT, here says  
" The `returnType` is optional, and if specified, can be any type that’s not an array, string, or handler. "

> **[Syntax - Legato Docs](https://docs.legato.io/latest/apiFilesSyntax.html#apiFilesSyntax_function)**
>
> legato™ is an open source Linux-based embedded platform designed to simplify connected IoT application development

Seems there is some limitation on returning STRUCT.

You can have a look on this test application IPC\_step1.rar.  
I have put the string array message2 as OUTPUT of the function.

> [@In legato 19.02 api is not supporting more than 2 string](https://forum.legato.io/t/in-legato-19-02-api-is-not-supporting-more-than-2-string/5716/2):
>
> how about making the API file like this? STRUCT ARRAY123 { string Array1[10]; int32 port1; string Array2[10]; int32 port2; string Array3[10]; int32 port3; string Array4[10]; int32 port4; string Array5[10]; int32 port5; }; FUNCTION arraySend ( ARRAY123 asd IN ); It is working on my side with vscode leaf swi-wp76\_4.7.0. You can use my code: [IPC\_step1.rar](https://forum.legato.io/uploads/short-url/dgUwkVXIXDa6sS6CbPxzjqn2x7a.rar) (2.4 KB)

---

<div class="post-metadata">

**Author:** ![jyijyi](https://sea1.discourse-cdn.com/flex019/user_avatar/forum.legato.io/jyijyi/32/822_2.png) [@jyijyi](https://forum.legato.io/u/jyijyi)\
**Post date:** [November 26, 2023, 8:10am UTC](https://forum.legato.io/t/returning-struct-from-api/6644/3 "2023-11-26T08:10:28Z")

</div>

I have written a sample application for you, it is not returning the STRUCT, but it can return the required value in the parameter.

[IPC\_step1\_api\_test.7z](https://forum.legato.io/uploads/short-url/nsDrojBBidshpQkuHnTsnq5AigZ.7z) (2.1 KB)
