----
This reference to a variable can be obtained by preceding
the identifier of a variable with an ampersand sign (&),
known as reference operator, and which can be literally
translated as "address of".
----
"Address of" is confusing, and is what confused me as a beginner, because there is no notion of type associated with an address.
If (&x) returns "the address of x," you'd be inclined to say that ((&x) + 1) is one byte higher than &x, even though the C compiler will calculate the actual answer based on the size of x.
If x is an array as in this example, you'd have to know that C does this arithmetic based on the entire length of the statically defined array, rather than a single element as it would for a dynamically defined array with a pointer to it.
Well, this is something which is very obvious to me. If you have a pointer to an int and increment it by one, it makes much sense to me that it advances the pointer to the next int and not just by one byte. Otherwise the usage of pointers as iterators would also not work.
I was taught in CS101, as I think just about everyone is, that the & operator means "the address of."
So parsing &x + 1 into English becomes:
"The address of x plus one."
Except that, more explicitly, we should say:
"The address of x plus one times the size of the type of x."
What you should say is so far removed from the C code appears to say (add 1 to something) that confusion is inevitable.
That the second term should depend on the implicit type information from the first term, especially when the "address" operator has precedence over the + operator, seems backward to me.
To me an address is just a number, it carries no type information. Translate to s-expressions:
(+ (address-of x) 1)
Does it make sense that the 1 would change value based on the result of "address-of"? Not to me. I think it's bad design. Now, if it were taught differently:
well sure it is confusing if you never used C (as most of the other language does not give access to pointers at all)
but when you see something tagged as 'C', 'pointers', 'challenge' I guess you can fairly assume it is an "advanced subject" for people already experienced in C no ?
and any C/C++ programmer who have spent few days with the language will never be confused by that
see http://www.cplusplus.com/doc/tutorial/pointers/
---- This reference to a variable can be obtained by preceding the identifier of a variable with an ampersand sign (&), known as reference operator, and which can be literally translated as "address of". ----