pecasting in c... *(long *) &array= 0x2342342;... i dont understand...

Discussion in 'C' started by max_payne, Feb 2, 2007.

  1. max_payne

    max_payne New Member

    Joined:
    Feb 2, 2007
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    hi everyone,

    i am a noob in c.
    i have problem understanding the following statement on a program:
    *(long *) &array= 0x2342342;
    this is supposed to fill a char array with an address in its ascii code form but can somenone please make it more clear to me?
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    Nor do I understand it. Does the program work as expected?
     
  3. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    http://www.daweidesigns.com
    The concept is to take an array of char and treat it as a long, thus transferring 4 bytes at once. The concept is dicey, as different microprocessors arrange the 4 bytes of a long differently, depending upon their endianess. As a matter of fact, the length of a long is implementation dependent.
     
  4. max_payne

    max_payne New Member

    Joined:
    Feb 2, 2007
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    hi dawei and thanx for replying!

    my prob wasnt about what this statement does althought you are 100% correct.
    this statement was used so that it could fill four places of a char array (four bytes length) with a long value (four bytes length)that looks like an address in hex form...

    now what i am having trouble is to understand the syntax that was used, meaning that strange typecasting...

    The way i see it is:
    &array means-> take the address of the i element in the array
    (long*) means-> cast it to a pointer for long numbers.
    *(long*)..... means-> the value of the above pointer.

    Is that correct???

    will it be the same if i used the following set of commands?

    array[0]='\x42'
    array[1]='\x23'
    array[2]='\x34'
    array[3]='\x02'

    (in reverse order because of the endian style????)

    p.g.: sooo thats way big guys use debugers!!!!!!
     
  5. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    http://www.daweidesigns.com
    The syntax is a little strange, since &array is the same as array is the same as &array[0] (array labels are treated much like pointers, since one can't treat an array as a single entity).

    &array is thus the address of the char array; it's a char *.
    (long *) casts it to a pointer to long, rather than a pointer to char.
    * (long *) &array dereferences it for storing the 0x02342342.

    However, as mentioned above, the order in which those values will be stored is implementation dependent, and it isn't necessarily just reversed. Storing them as you show (single values) might not result in what you actually want.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice