0

Suppose I have an API function which returns a pointer to some memory, that is later to be consumed in an array-like manner. Suppose also that its size is known in advance, and that it's declared like so:

returntype API(void);

Is it better for the function's return type to be declared as pointer to an array, like so:

typedef struct data (*returntype)[];

or for it to be defined as simply the good old pointer to the first element, like so:

typedef struct data *returntype;

On one hand returning a pointer to an entire array seems to make more sense, but on the other the API consumer would probably prefer the 2nd version because it's easier to consume:

e.g. from the consumer's perspective, copying the 3rd struct in the array:

1st declaration:

returntype = API();
struct data third= (*returntype)[2];

2nd declaration:

returntype = API();
struct data third = returntype[2];
4

The defacto standard would be option 2: to return a pointer to the first element of the array.

Your first option is actually a pointer to a pointer to the first element of the array: typedef struct data** returntype; You can of course do this, but that second level of indirection implies a second order array. ie: typedef struct data (returntype)[][]; Which is perfect if your data fits that description.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.