Sign in to follow this  
dave

Is this legal syntax?

Recommended Posts

dave    2187
Hi, I was looking at some source i found online and i saw a code segment where there were 4 '&' ampersands in a row, no white space. Is this legal syntax? ace

Share this post


Link to post
Share on other sites
Unwise owl    158
Just tried something like this after getting curious:

int a = 1;
int* b = &a;
int** c = &&a;
int*** d = &&&a;
int**** e = &&&&a;




Doesn't seem to work...

Nor does:

int a = 1;
int* b = &a;
int** c = &(&a);
int*** d = &(&(&a));
int**** e = &(&(&(&a)));




(Returns same error as if I whitespace the ampersands.)

Only int**c = &b; etc. works. Not too surprising as I'd expect an expression such as &a only to generate a value at a temporary address, which in turn you cannot take the address of.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
Quote:

Hi, I was looking at some source i found online and i saw a code segment where there were 4 '&' ampersands in a row, no white space. Is this legal syntax? ace


Loved the puzzle but a simpler solution, though not what you expect as an answer (but don't you love it when you find a loophole to a question?):

int main{
char a[10] = "&&&&";
printf("This is legal %s",a);
return 1;
}

Share this post


Link to post
Share on other sites
ZQJ    496
Well, in gcc the following compiles:


int main()
{
bool value = true &&&& label;
label:
return 0;
}


That's actually true && (&&label), where &&label gets the address of the label. It's a gcc extension though, don't know about other compilers.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this