Thread: Casting character point into structure

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    13

    Question Casting character point into structure

    Hi,

    I am trying to directly cast character pointer to typedef structure which has different types of member.

    Code:
    typedef struct
    {
      char a[10];
      int b;
      char c[10];
    }MyStruct;
    
    int main()
    {
    char *ptr;
    MyStruct *pstruct;
    
    /* or C style casting: */
     memset(&ptr, '\0', sizeof(1972));
    
     ptr = "12394678901234567890testnode123456\0 eventry\0        567\0y8901234567z8";
    
    pstruct = (MyStruct *) ptr;
    
    fprintf(stderr, "value of b %u\n", pstruct->b);
    
    return 0;
    }
    The value of b is printed as some number which is not as per the value of ptr after 10 character so not sure where the value its printing from.

    Can you suggest what could be the problem?

    Thanks.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    What exactly do you expect it to print, and what do you actually get?

    As far as I can tell, you should get 0x31323334, which is 825373492 in decimal.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    13
    I expect to print the value from a string which is assigned to integer variable after casting...

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Given probable struct padding, a little endian machine, and 32 bit ints you'd get: 0x36353433 (909,456,435 decimal) which corresponds to the "3456" part of the string.

    The memory layout of your struct probably doesn't match up with what you think it should be when you cast the char pointer. Given the assumptions above what is likely happening is that the "1239467890" part of the string gets "correctly placed into the a struct member. Then, due to padding, the next "12" are skipped. Then the int comes into play occupying the memory which is taken up by the "3456" string (which when converted to little endian and displayed as an int end up as show at the beginning of this post). Next, the "7890testno" part is placed where the c struct member should be.
    Last edited by hk_mp5kpdw; 05-28-2009 at 06:06 AM.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    13
    Even if the int is kept as first member of the structure still it prints as some garbage value.

    for instance in this case, instead of printing "12345" it prints "12594" and I dont have any clue where this value is picked up from.

    is above code correct where I am accessing the integer member value of structure directly?

    Thanks.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Ok, so you are presumably using Turbo C or some such that has a 16-bit integer.

    Integer values are not stored as text in memory, they are stored as binary values. 12954 is the same as 0x3231 - the digits 2 and 1 combined to a 16-bit value [and hk_mp5kpdw is right, I got the byte-order wrong, and if the numbers were 32-bit, alignment would have also become a factor].

    You would need to convert the numeric text into a binary value to achieve what you want. Look into fucntions like sscanf() and strtol().

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User
    Join Date
    May 2009
    Posts
    13
    I have found the code which flip the bytes before printing or using it but the code dumps with segmentation fault. The following is the code :
    Code:
    void FlipByte16 (UInt16 *FlipIntPtr)
    {
        UInt8 *TempBytePtr = (UInt8 *) FlipIntPtr;
        UInt8 TempByte;
    
        /********************************************************************/
        /*  Bytes are ordered AB, so flip A and B as follows:               */
        /*      1.  Move A to TempByte.                                     */
        /*      2.  Move B to A.                                            */
        /*      3.  Move TempByte to B.                                     */
        /********************************************************************/
        TempByte = TempBytePtr[0];
        TempBytePtr[0] = TempBytePtr[1];
        TempBytePtr[1] = TempByte;
    }
    Here I am passing the address of the integer variable to this function which then points me to the reqiured data from the string.
    [CODE]
    The code - TempBytePtr[0] = TempBytePtr[1]; dumps with the fault.
    [/QUOTE]
    Can you please help?

    Thanks,
    Shail

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    What are you feeding into that function? A string literal? E.g.
    Code:
    char *s = "Hello";
    FlipBytes(s);
    will not work, because the string "Hello" is in read-only memory.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    Registered User
    Join Date
    May 2009
    Posts
    13
    I am feeding the same string as mentioned in my first post which is as follows:


    ptr = "12394678901234567890testnode123456\0 eventry\0 567\0y8901234567z8";

    Thanks,
    Shail

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    That's a string literal, which means the contents pointed to are not changeable.

  11. #11
    Registered User
    Join Date
    May 2009
    Posts
    13
    I am passing pointer to integer variable which is part of structure member.

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I thought you said you were passing
    Code:
    ptr = "12394678901234567890testnode123456\0 eventry\0 567\0y8901234567z8";
    which is a pointer to a string literal.

    Just paste in your code and we'll look at it.

  13. #13
    Registered User
    Join Date
    May 2009
    Posts
    13
    Code:
    UInt16 Type = "12594678901234567890testnode123456"
    
    FlipByte16(&Type);
    
    void FlipByte16 (UInt16 *FlipIntPtr)
    {
        UInt8 *TempBytePtr = (UInt8 *) FlipIntPtr;
        UInt8 TempByte;
    
        /********************************************************************/
        /*  Bytes are ordered AB, so flip A and B as follows:               */
        /*      1.  Move A to TempByte.                                     */
        /*      2.  Move B to A.                                            */
        /*      3.  Move TempByte to B.                                     */
        /********************************************************************/
        TempByte = TempBytePtr[0];
        TempBytePtr[0] = TempBytePtr[1];
        TempBytePtr[1] = TempByte;
    }

  14. #14
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by shaileshk View Post
    Code:
    UInt16 Type = "12594678901234567890testnode123456"
    "12594678901234567890testnode123456" is very far from resembling a 16 bit unsigned integer.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  15. #15
    Registered User
    Join Date
    May 2009
    Posts
    13
    If you look at my original post you will see where I am coming from. This alll value is getting cast from string to integer and all I am doing is printing the value of integer.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help realy needed
    By ZohebN in forum C++ Programming
    Replies: 2
    Last Post: 04-08-2008, 09:37 AM
  2. Grammar to FSA to C code (Newbie)
    By aybe in forum C Programming
    Replies: 4
    Last Post: 02-29-2008, 02:10 PM
  3. How accurate is the following...
    By emeyer in forum C Programming
    Replies: 22
    Last Post: 12-07-2005, 12:07 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. trouble with overloaded operator
    By kkurz in forum C++ Programming
    Replies: 2
    Last Post: 10-31-2003, 12:59 PM

Tags for this Thread