Typecasting question

This is a discussion on Typecasting question within the C++ Programming forums, part of the General Programming Boards category; Greetings all, In the code below, can someone explain the char ** line? I think it assigns the value 0 ...

  1. #1
    Registered User
    Join Date
    Jan 2010
    Posts
    2

    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.

    Code:
    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
    Posts
    5,851
    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%.

  3. #3
    Registered User
    Join Date
    Jan 2010
    Posts
    2

    Thumbs up Thanks

    Grumpy,

    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.

    Cheers,
    Hoksila

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 Cprogramming.com
    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, 12:36 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 12:47 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21