Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return pointer to array from function with array dimensions after function name

I came across this odd function declaration in my code base that I would like help understanding:

struct MemberStruct (*GetMember ( 
    CONTAINER_STRUCT *Buffer 
    ))[DIM_1][DIM_2][DIM_3]
{
  return(&Buffer->MemberStructArray);
}

It behaves like a pseudo-accessor. It returns the address of the MemberStruct array within CONTAINER_STRUCT.

CONTAINER_STRUCT has this definition:

typedef struct ContainerStruct {
  // Other members
  struct MemberStruct        MemberStructArray[DIM_1][DIM_2][DIM_3];
  // Other members
} CONTAINER_STRUCT;

This function is called like this:

// declarations at the top of a function
struct MemberStruct (*MemberStructArray)[DIM_1][DIM_2][DIM_3];
CONTAINER_STRUCT Container;

// Other code, including the initialization of Container

MemberStructArray = GetMember(&Container);

I'd like to better understand the function signature, and haven't been able to find any examples of this construct online. My specific questions are:

  1. How do the array dimensions after the name of the function work? How do they relate to the return type when the function name is between the return type and the dimensions?
  2. Why is the * symbol inside the parenthesis with the function name? Since this is returning an address, shouldn't the reference operator be bound to the return type rather than the function name?
like image 563
skrrgwasme Avatar asked Apr 30 '26 09:04

skrrgwasme


1 Answers

Breaking up the declaration: GetMember is a function:

GetMember()

That takes one parameter of type CONTAINER_STRUCT *:

GetMember(CONTAINER_STRUCT *)

And returns a pointer:

*GetMember(CONTAINER_STRUCT *)

To a 3D array:

(*GetMember(CONTAINER_STRUCT *))[DIM_1][DIM_2][DIM_3]

Of struct MemberStruct:

struct MemberStruct (*GetMember(CONTAINER_STRUCT *))[DIM_1][DIM_2][DIM_3]
like image 84
dbush Avatar answered May 02 '26 05:05

dbush



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!