-1

For practice, I'm trying to :

Declare fubar to be a pointer to a function that takes a pointer to a char and returns a pointer to an array of 24 elements where each element is a pointer to a struct foo.

My logic is:

-fubar is a pointer to a function taking a char pointer: (*fubar)(char*)

-...returning a pointer to an array of 24 elems of where each elem is a struct foo:

(struct foo *)(*fubar)(char*)[24]

Is my logic correct?

5
  • 1
    No. The parentheses make a big difference. It should be struct foo* (*fubar)(char*)[24] for this to be valid syntax. Commented Nov 25, 2014 at 8:50
  • 2
    Do you know cdecl (cdecl.org)? Commented Nov 25, 2014 at 8:50
  • Yes I do know cdecl but it was saying syntax error but I didnt know why. Commented Nov 25, 2014 at 8:57
  • (struct foo *) is a typecast, which makes no sense in this case. You could have tried to remove the parentheses: cdecl.ridiculousfish.com/?q=struct+foo+*%28*fubar%29%28char*%29[24] and then it's not a syntax error anymore. Commented Nov 25, 2014 at 9:25
  • @mch ahhhh i see! Thanks for adding that comment, I think I'm finally understanding it more Commented Nov 25, 2014 at 9:33

2 Answers 2

1

After fixing the syntax error and removing the parentheses around struct foo *,

struct foo* (*fubar)(char*)[24]

...the one part that you got wrong is that it actually returns an array of pointers, not a pointer to an array. In order to declare a pointer to the array, you need an extra set of parentheses:

struct foo (*(*fubar)(char*))[24]

You can pretend that that the star belongs to an identifier (i.e., the name of the array) inside the parentheses.

Sign up to request clarification or add additional context in comments.

4 Comments

quick question: will removing the outer parentheses change the statement? Is struct foo *(*fubar)(char*)[24] the same?
@Penu Like I said, the parentheses matter. Fixing the syntax error leads to the form you just described, which returns an array of pointers. The parentheses change the binding of complex declarators.
Thanks. I've been thinking but should the answer in fact be: struct foo *(*(*fubar)(char*))[24]
@penu No, that returns a pointer to an array of pointers. I was presenting syntax that demonstrates what it would look like if your understanding had been correct.
1

Functions never return arrays, they may return a pointer (conventionally to the first cell of an array), since as return value or as argument an array decays to a pointer.

So, declare with a typedef the signature of your function:

typedef struct foo** funsig_t(char*);

notice that if you omit the typedef you would declare a function funsig_t of the desired signature.

then declare a pointer using that typedef:

funsig_t* fubar;

Comments

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.