• Advertisement
Sign in to follow this  

Is this legal syntax?

This topic is 4476 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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
Advertisement
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
To infinity and beyond....

T& T::operator&()
{
return *this;
}

void test()
{
T t1;
T t2 = &(&(&(&(&(&t1)))));
}



Although it doesn't work without the brackets.

Share this post


Link to post
Share on other sites
Ok Joanus, show me a code sample that works with what you have written, no whitespace remember. You can't go to infinity with that...

ace

Share this post


Link to post
Share on other sites
Was putting the example in my post as you replied. Checked the link afterwards and was indeed wrong, but the above does compile [smile]

Share this post


Link to post
Share on other sites
you provide a correct solution but since i didn't ask what i meant, i mean no white spaces or any other character.

ace

Share this post


Link to post
Share on other sites
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
You are right, but obviously not the kind of answer we're getting at because it is so blatent.

ace

Share this post


Link to post
Share on other sites
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
Sign in to follow this  

  • Advertisement