passsing &array same as passing array ?

O

Oeleboele

OK, I can't understand this exactly:

I have this function: int howdy(void *)

I first past this to the function &aCharArray
I realized later that this should be aCharArray but...
the former version also worked ?

I can't understand why that worked. The only thing I can come up with
is that the compiler gave me a break (although I did not receive any
warnings) Or perhaps the compiler does something like this :
&(aCharArray[0]) ?

Everything worked & works ok but I'm just currieus why.

Greetings, Hans
 
J

Jens.Toerring

Oeleboele said:
OK, I can't understand this exactly:
I have this function: int howdy(void *)
I first past this to the function &aCharArray
I realized later that this should be aCharArray but...
the former version also worked ?
I can't understand why that worked. The only thing I can come up with
is that the compiler gave me a break (although I did not receive any
warnings) Or perhaps the compiler does something like this :
&(aCharArray[0]) ?
Everything worked & works ok but I'm just currieus why.

Normally, whenever an array appears in an expression the compiler
implecitely converts this into a pointer to the first element of
the array - but there's an exception, and that's when you take
the address of the array (or use it as the operand of sizeof),
so 'a' and '&a' (assuming a is an array) evaluate to the same
thing. It's mentioned in section 6.3 of the FAQ and specified in
section 3.2.2.1 of the C89 standard and section 6.3.2.1 of C99
(or at least in the drafts):

Except when it is the operand of the sizeof operator or the
unary & operator (...) an lvalue that has type "array of
type" is converted to an expression that has type "pointer
to type" that points to the initial member of the array
object and is not an lvalue.

But most compilers should give you warning if you do that.

Regards, Jens
 
C

Chris Torek

This is expectable, and perhaps even required (it is hard to tell).

Normally, whenever an array appears in an expression the compiler
implecitely converts this into a pointer to the first element of
the array - but there's an exception, and that's when you take
the address of the array (or use it as the operand of sizeof),

Or put it in any other "non-value-context" (but there really are
no other useful ones; the useless ones are things like operands of
++ or --, or LHS of assignment, where naming an array object is an
error).
so 'a' and '&a' (assuming a is an array) evaluate to the same
thing.

Not quite: they differ in *type* (much as (int)1 and (double)1.0
differ in type, even though they are both "the number 1").

Since the declaration of howdy() in ">>" above says that it takes
a value of type "void *", and any object-pointer-type can be
converted to "void *" and back, it is unsurprising that passing
&aCharArray[0] and passing &aCharArray give the same result: both
are "arrows" pointing to the same first byte, they just point to
a different total *number* of bytes. Conversion to "void *" throws
away the "number of bytes" information, leaving you with two raw
byte-pointers pointing to the same (single) byte.
It's mentioned in section 6.3 of the FAQ and specified in
section 3.2.2.1 of the C89 standard and section 6.3.2.1 of C99
(or at least in the drafts) ...

See also <http://web.torek.net/torek/c/pa.html>.
 
J

Jens.Toerring

This is expectable, and perhaps even required (it is hard to tell).
Or put it in any other "non-value-context" (but there really are
no other useful ones; the useless ones are things like operands of
++ or --, or LHS of assignment, where naming an array object is an
error).
Not quite: they differ in *type* (much as (int)1 and (double)1.0
differ in type, even though they are both "the number 1").

Thanks, that was something I wasn't sure about - but that explains
the warning one gets (about an "incompatible pointer type" in the
function call) if one doesn't make howdy() to expect a void pointer
but a pointer to the type of the elements of the array.

Regards, Jens
 
B

Barry Schwarz

OK, I can't understand this exactly:

I have this function: int howdy(void *)

I first past this to the function &aCharArray
I realized later that this should be aCharArray but...
the former version also worked ?

I can't understand why that worked. The only thing I can come up with
is that the compiler gave me a break (although I did not receive any
warnings) Or perhaps the compiler does something like this :
&(aCharArray[0]) ?

Everything worked & works ok but I'm just currieus why.

Everything worked as expected because void* is compatible with a
pointer of any other type. When you passed &aCharArray, the argument
was of type pointer to array of char and was implicitly converted to
void*. When you passed aCharArray, the argument was of type pointer
to char and was implicitly converted to void*. Since both "types"
referred to the same address (had the same value), when each was
converted to void* the value and type received by the function was the
same.

If your function had been expecting a char*, the & argument would have
required a diagnostic during compilation. This is because pointer to
char and pointer to array of char are not compatible for implicit
conversions.


<<Remove the del for email>>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,602
Members
45,184
Latest member
ZNOChrista

Latest Threads

Top