Thread: Typecasting question

  1. #1
    Registered User
    Join Date
    Jan 2010

    Exclamation Typecasting question

    Greetings all,

    In the code below, can someone explain the char ** line?
    I think it assigns the value 0 to an astructure[foo] array element. I just don't understand why char in (char **) is used. It is causing gcc to barf up a "dereferencing type-punned pointer will break strict aliasing rules" warning. I could type-cast it again to void, however I dont know why it was casted to char to begin with. The segment of code is part of a huge project I am working on, that is quite established.

    struct mystruct
           u_int16_t foo;
           u_int16_t bar;
    struct mystruct *astructure;
     (*(char **)&astructure)[foo] =0;

  2. #2
    Registered User
    Join Date
    Jun 2005
    It sets the foo'th character of the pointer named astructure to zero.

    The [foo] on that line is unrelated to the member named foo in struct mystruct - presumably the name of another variable.

    I suspect it is a hack that was found to achieve some desirable effect on some target machine. Subsequent and preceding usage of astructure might give some clue about what the purpose and desired effect is.

    I further suspect the desired effect - whatever it is - is only achieved on a machine with a particular endianness (eg it might work on a little endian machine, but not on a big endian or middle endian machine).
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Jan 2010

    Thumbs up Thanks


    Thanks for the reply. Your assumptions were almost entirely correct. The source was written with a specific machine in mind, and included conversion utilities for endianness, (htons(), htonl(), ntohs(), ntohl())

    What was confusing me was the member variable and pointer foo. Poor naming scheme, and/or a lack of attention to detail on my part.


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  2. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  3. Question...
    By TechWins in forum A Brief History of
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  4. Question about linked lists.
    By cheeisme123 in forum C++ Programming
    Replies: 6
    Last Post: 02-25-2003, 01:36 PM
  5. Question, question!
    By oskilian in forum A Brief History of
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM

Tags for this Thread