passsing &array same as passing array ?

Discussion in 'C Programming' started by Oeleboele, Aug 6, 2004.

  1. Oeleboele

    Oeleboele Guest

    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
    Oeleboele, Aug 6, 2004
    #1
    1. Advertising

  2. Oeleboele

    -berlin.de Guest

    Oeleboele <> wrote:
    > 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
    --
    \ Jens Thoms Toerring ___ -berlin.de
    \__________________________ http://www.toerring.de
    -berlin.de, Aug 6, 2004
    #2
    1. Advertising

  3. Oeleboele

    Chris Torek Guest

    >Oeleboele <> wrote:
    >> 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. ...


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

    In article <>,
    <-berlin.de> wrote:
    >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>.
    --
    In-Real-Life: Chris Torek, Wind River Systems
    Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
    email: forget about it http://web.torek.net/torek/index.html
    Reading email is like searching for food in the garbage, thanks to spammers.
    Chris Torek, Aug 6, 2004
    #3
  4. Oeleboele

    -berlin.de Guest

    Chris Torek <> wrote:
    >>Oeleboele <> wrote:
    >>> 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. ...


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


    > In article <>,
    > <-berlin.de> wrote:
    >>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").


    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
    --
    \ Jens Thoms Toerring ___ -berlin.de
    \__________________________ http://www.toerring.de
    -berlin.de, Aug 6, 2004
    #4
  5. On 6 Aug 2004 02:04:47 -0700, (Oeleboele) wrote:

    >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>>
    Barry Schwarz, Aug 7, 2004
    #5
  6. Oeleboele

    Oeleboele Guest

    Thank you all for your help. It makes it all clear.
    Oeleboele, Aug 9, 2004
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. David Hubbard
    Replies:
    2
    Views:
    813
    David Hubbard
    Jan 12, 2006
  2. JW
    Replies:
    1
    Views:
    431
    David Jeske
    Jul 12, 2003
  3. J W
    Replies:
    0
    Views:
    379
  4. =?Utf-8?B?Vmlua2k=?=

    passsing a changed variable value in javascript

    =?Utf-8?B?Vmlua2k=?=, May 5, 2007, in forum: ASP .Net
    Replies:
    0
    Views:
    330
    =?Utf-8?B?Vmlua2k=?=
    May 5, 2007
  5. deadpickle
    Replies:
    1
    Views:
    946
    Jens Thoms Toerring
    Nov 7, 2010
Loading...

Share This Page