3

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?
2
  • The clockwise/spiral rule is always useful to know. As is cdecl.org Commented Jul 26, 2018 at 19:52
  • @Someprogrammerdude cdecl.org chocked on this when I tried. But I'll review the sprial rule again. Thanks for the link. Commented Jul 26, 2018 at 20:03

1 Answer 1

3

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]
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the breakdown. I suppose the answer to any followup questions will be "because that's the way C is". It's the "To a 3D array" part that throws me off. It doesn't make sense to me for the array dimensions to be outside the parenthesis and on the right-hand side of the function name. That looks like an array of function pointers to me.
@skrrgwasme: If a[N] is an array, then *ap[N] is an array of pointers and (*pa)[N] is a pointer to an array. Replace pa with f(), and you get (*f())[N], or a function returning a pointer to an array. And yeah, this is how C declarator syntax works. Function declarator syntax is similar - if f() is a function, then *fp() is a function returning a pointer, and (*pf)() is a pointer to a function. Replace pf with a[N], and you have an array of pointers to functions - (*a[N])().

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.