-7

So I am new to C just came from Java and I wanted to understand how to read declarations. I know I gave one example but I want to be able to read all declarations and understand it clearly. I'm not really asking you guys to solve it but just guide me

For example:
            int point *[];
            int point *()[];
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
  • 7
    Check this out: http://cdecl.org/ – juanchopanza Apr 03 '14 at 20:13
  • 2
    We are not here to be your teachers. You can get this from pretty much ANY book on C. I'd recommend starting with the very first one: [K&R C](http://en.wikipedia.org/wiki/K%26R_C#K.26R_C) – Marc B Apr 03 '14 at 20:13
  • 2
    If you are "new to C", don't tag C++ unless you specifically are asking about similarities or differences. While it might not make a difference here, the languages and their idiomatic solutions to problems can be very different. – crashmstr Apr 03 '14 at 20:14
  • I don't think either of your examples are valid C declarations. `int *point[];` would be fine, but `int point *[];` doesn't make sense. `*` needs to modify a type -- `foo *` means "pointer to an object of type 'foo'" -- but `point` here is a variable name, not a type. – Caleb Apr 03 '14 at 20:23
  • 1
    See http://stackoverflow.com/questions/20635774/whats-the-difference-between-the-types-int-and-int-100-in-c/20647498#20647498 – Eric Lippert Apr 03 '14 at 20:39

1 Answers1

1

For deciphering C declarations use the Right - Left Rule

For your example int point *[]; is read as point is array of pointer to int.

First understand what the symbols mean.

* // "pointer to"
[] // "array of"
() // "a function returning"

Now that you know the symbols you can decipher any declaration.

For example int *(*pointer())();`

Step 1 Find the Identifier

int *(*pointer())();
       ^^^^^^^

In this case it is pointer, so you read as "pointer is"

Step 2 Use Right-Left Rule

int *(*pointer())();
             ^^

You move right first, and you'll hit a (), so you read as pointer is function returning

Step 3 Continue the rule

    int *(*pointer())();
          ^

Then you move right again, but here you have a closing parenthesis ), so you move left of the identifier and here you have a *, so you read as pointer is function returning pointer to

Step 4 Continue the rule

int *(*pointer())();
                 ^^

Then you keep moving left, but there is a opening parenthesis (, so you can't move left, so now you move right. Now you reached the (), you read as pointer is function returning pointer to function returning

Step 5 Continue the rule

int *(*pointer())();
    ^             

Now you can't move right because there are no more symbols, so move left and reach *, you read as pointer is function returning pointer to function returning pointer to

Step 6 Continue the rule

int *(*pointer())();
^^^

Now you can't move left or right because you're out of symbols so you read int, you finally have pointer is function returning pointer to function returning pointer to int

Moonhead
  • 1,570
  • 8
  • 17