strange function-like syntax in c and c++

Viewed 99

The following compiles:

main()
{
    int(asdf);

}

It seems this is some strange kind of declaration. I have tried to find code like this, but was unable to. Could someone explain?

1 Answers

It turns out the line

int(asdf);

is equivalent to

int asdf;

which obviously declares an ordinary local variable named asdf.

But you can put parentheses around various parts of the declarator, whether you need to or not. So it's just the same if you write

int asdf;

or

int (asdf);

or

int ((asdf));

Parentheses are allowed in declarators because, sometimes, they're necessary in order to make a significant distinction. For example,

int *ap[10];

declares an array of 10 pointers, while

int (*pa)[10];

declares a pointer to an array.

Related