Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

strange function-like syntax in c and c++

Tags:

c++

c

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?

like image 880
S.Sot Avatar asked Oct 16 '25 12:10

S.Sot


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.

like image 126
Steve Summit Avatar answered Oct 19 '25 02:10

Steve Summit



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!