3

In objective-c, I really cannot understand this:

void *x = &x;

my understanding is:

declare a generic pointer (hence type void*), the pointer variable name is x, and this pointer points to a reference to a variable (that should be declared already- but it is not) named x.

very confusing to me!

Muhammad Hewedy
  • 29,102
  • 44
  • 127
  • 219
  • Please add comments as comments to the answer you want to comment on. Or also add an answer to your own question. – alk Sep 05 '14 at 09:54
  • "that should be declared already- but it is not" - yes it is. The point of declaration comes *before* the initialiser. Once you understand that, the meaning should be clear. – Mike Seymour Sep 05 '14 at 10:54
  • Related: http://stackoverflow.com/q/16270323/694576 – alk Sep 05 '14 at 10:55

3 Answers3

5

To conclude whether this declaration

void *x = &x;

is valid you should take into account two important quotes from the C Standard.

the first one says where ths scope of an identifier starts (6.2.1 Scopes of identifiers)

7 Structure, union, and enumeration tags have scope that begins just after the appearance of the tag in a type specifier that declares the tag. Each enumeration constant has scope that begins just after the appearance of its defining enumerator in an enumerator list. Any other identifier has scope that begins just after the completion of its declarator.

The second one says whether a pointer of any type can be assigned to a pointer to void (6.3.2.3 Pointers)

1 A pointer to void may be converted to or from a pointer to any object type. A pointer to any object type may be converted to a pointer to void and back again; the result shall compare equal to the original pointer.

So in this declaration

void *x = &x;

the scope of variabble x starts immediately before the assignment operator. Its complete type is void * and it can be assigned any pointer of other type. In the right hand there is expression of type void **. And according to the second quote it can be assigned to x because x is a pointer to void.

As result x will store the address of itself.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • Thanks, the part about where the scope of the identifier begins is indeed exaclty what answers the question. – JBL Sep 05 '14 at 11:06
2

For C and C++:

x gets assigned its own address. It points to itself afterwards.

alk
  • 69,737
  • 10
  • 105
  • 255
  • That doesn't explain how you can assign the address of a variable to itself (in this case where it is a pointer, of course) on the same line you declare it :/ – JBL Sep 05 '14 at 09:51
  • So it's completely up to the compiler to figure out this special case? I can't find anything relevant in the standard that would explain this case. (Then, that's far from trivial I'd say). – JBL Sep 05 '14 at 09:57
0

Based on understanding of @alk answer:

It is exactly as you say:

int y = 10;
void* x = &y;

But in out case, x points to itself instead of y

Muhammad Hewedy
  • 29,102
  • 44
  • 127
  • 219