Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding some crazy C/C++ declarations [duplicate]

Tags:

c++

c

Possible Duplicate:
How to understand complicated function declarations?

Consider:

char (*(*x())[5])()

How do I dissect this expression?

I think it is a function which return an array of size 5, whose members are pointers to function which receive no input and return a char.

Am I right?

like image 514
Jichao Avatar asked Jul 10 '12 09:07

Jichao


People also ask

What is the right left rule in C?

The right-left rule [Important] This is a simple rule that allows you to interpret any declaration. It runs as follows: Start reading the declaration from the innermost parentheses, go right, and then go left.

What is mean of Int A 10 ];?

int a[10]; refers to 10 cells of integers allocated in memory. int *b = a; is equivalent to int *b = &a[0]; and means that b points to the first cell of a to be precise.


2 Answers

Search for "Right-left rule"

In your case, it should be:

         x          : x is a
         x()        : function
        *x()        : returning pointer to
       (*x())[5]    : a 5-element array of 
      *(*x())[5]    : pointer to
     (*(*x())[5])() : function
char (*(*x())[5])() : returning char
like image 58
Adrian Shum Avatar answered Oct 07 '22 14:10

Adrian Shum


I have been seeing a lot of weird declarations on Stack Overflow these days.

When I'm lazy, I use cdecl.org:

"declare x as function returning pointer to array 5 of pointer to function returning char"

When not, there is the clockwise spiral rule. <- It is AWESOME

like image 25
ArjunShankar Avatar answered Oct 07 '22 14:10

ArjunShankar