Function returns a pointer to an array of pointers.

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Function returns a pointer to an array of pointers.

1,645 Views
FWFan
Contributor III

Hi Guys,

 

Have you done something like that before?

I am trying to return a pointer to an array of pointers

from one of my function.  How should I type

return?  Like "return *ptrToArryPtr"?  Because

"return ptrToArryPtr" does not work.

 

Thank you in advance.

FWFan

Labels (1)
0 Kudos
4 Replies

425 Views
CompilerGuru
NXP Employee
NXP Employee

Not sure I understand the problem. Is it that ANSI-C does not support to return arrays (or pass in arrays per value for that matter?).

If you need to return an array either pass in the memory area to write the array into (best solution if you can, the main problem is that the caller needs to know the size of the array before the call), return a struct containing an array (main problem is that this does fix the array size at compile time), return a pointer to the array (which raises the question where the memory the pointer points to is allocated, does not work with stack as the function returns, using globals is problematic with multi-threading and lifetime of the buffer, using heap is problematic in embedded as well, so that might or might not be a choice).

 

So either way of returning an array has its problems, which is the best depends on what you try to pass back. Note that this is a general issue in C independent of what element-type the array has. Arrays of pointers just make the syntax a bit more complex.

 

Daniel

 

0 Kudos

425 Views
FWFan
Contributor III

Thank you Daniel.

 

I have tried returning a pointer to an array before and it works.  But now I need to return a pointer to and array of pointers.  This is because I will be working with a lot of data.  I think that is what I need to know, the syntax of how to do it.  I tried "return *ptrToPtrsArry", does this look familiar?

 

Thank you,

FWFan

0 Kudos

425 Views
CompilerGuru
NXP Employee
NXP Employee

With this little information it is pretty much impossible to say if it is ok, really need much more information like what the prototype of the function is, how ptrToPtrsArry is declared, how the array being returned is allocated (most critical here), how the array is used, if there are threading constaints and more.

 

Basically returning an array of pointer is in no respect different than returning an array of non pointer elements.

For both cases using an expression like "return *ptrToPtrsArry" probably means that ptrToPtrsArry is defined with one indirection too much, with one indirection which does not add any value, just complicates matters and slows everything down a bit.

Not that in C with arrays usually a pointer to the first element of the array is used and not a pointer to an array.

Say with the array 

> int my_array[10];

a variable of type

> int* ptr = my_array;

is used to refer to the array and not a pointer to an array:

Not used typically: > int (*ptr_to_array)[10] = &my_array;

 

Daniel

0 Kudos

425 Views
FWFan
Contributor III

Thank you Daniel. 

I will continue to use pointer to an array as you suggested.

I will save the other topic for future reference.

 

Much appreciated.

FWFan

0 Kudos