1

I know that int *(*func)() means a function pointer which takes no arguments and returns an integer pointer but what does the following syntax means in C:

int *(*func())[]

Any explanations on how do we read such syntax in C would be very helpful.

BaluRaman
  • 265
  • 5
  • 16
  • 4
    According to http://cdecl.org/ that's not valid syntax. Are you sure that that's what is written? (N.B.: http://cdecl.org/ is a site that translates C declarations to English). – AntonH Mar 23 '17 at 14:12
  • 1
    Someone correct me, but AFAIK an empty parameter list in `C` does not mean *"takes no arguments"*, this would be true in `C++` not in `C`. – A.S.H Mar 23 '17 at 14:18
  • 3
    Okay, just realised that `func` appears to be a reserved keyword on http://cdecl.org/. Anyway, entered `int *(*foo())[]`, and it says **declare foo as function returning pointer to array of pointer to int**. – AntonH Mar 23 '17 at 14:24
  • @AntonH That would be because that site is pretty bad and fails at numerous cases of valid C code. Instead of linking that bad, irrelevant site, try to compile the code with your standard compliant compiler. Works just fine since it is valid C code. Why 4 people like your comment, I don't know. – Lundin Mar 23 '17 at 15:12

2 Answers2

3

func is a function and returning a pointer to an array of pointers to int.

reference link : http://gateoverflow.in/35193/regarding-pointers

msc
  • 33,420
  • 29
  • 119
  • 214
  • 3
    hmm, not really. If no void is explicitely written as the function parameter, this does not mean it takes no arguments in C. Even more, this means the function can have any number of parameters of any type. Here was also a comment about that above from @AntonH. – dmi Mar 23 '17 at 14:25
  • @cmaster No, it is an array pointer. Array pointers don't decay. – Lundin Mar 23 '17 at 15:08
  • @Lundin But this looks dangerously close to the `int foo(int bar[])` case, where the declaration is definitely equivalent to `int foo(int* bar)`. Since there is no array variable declared in `int* (*func())[]`, I would guess that should be equivalent to `int** (*func())`. After all, you can't pass around an array in C, you can only ever pass a pointer to its first element. – cmaster - reinstate monica Mar 23 '17 at 15:29
1

It is a function that returns an array pointer. Written more explicit, here is the meaning of the various parts:

item_type (*function_name(parameters))[array bounds];

Where item_type is the type of the array items in the array pointed at.

The array pointer is a pointer to an array of unknown size, which is actually allowed by C, but not overly useful.

Overall, this function doesn't seem very useful and if you should ever need such an odd construct, you shouldn't write it as gibberish like this. Here is the equivalent form with a typedef:

typedef int* array_t[];

array_t* func();
Community
  • 1
  • 1
Lundin
  • 195,001
  • 40
  • 254
  • 396