Thread: Casting character point into structure

  1. #16
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by shaileshk View Post
    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.
    If by "cast from string to integer" you mean "using the location in memory of the string as an integer", then fine.

  2. #17
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by tabstop View Post
    If by "cast from string to integer" you mean "using the location in memory of the string as an integer", then fine.
    Which AFAICT that is *not* what is happening. In that case, it might as well be:

    Code:
    Uint16 Type="";
    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

  3. #18
    Registered User
    Join Date
    May 2009
    Posts
    13
    I have checked the value of Type using gdb as debugger on core file and the value of Type is "12594678901234567890testnode123456"

    Ta,
    Shail

  4. #19
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by MK27 View Post
    Which AFAICT that is *not* what is happening. In that case, it might as well be:

    Code:
    Uint16 Type="";
    I probably wasn't very clear. What you have would be just as good as what he has, in terms of what's actually happening. I have no idea what we wants to have happen (run strtoul on the string maybe?) but it's doubtful that that's what he wants.

  5. #20
    Registered User
    Join Date
    May 2009
    Posts
    13
    This is not string but integer but all the problem happening is am unable to print the value of integer structure member and thats why I am trying to Flip the byte and getting it to work.

  6. #21
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by shaileshk
    This is not string but integer but all the problem happening is am unable to print the value of integer structure member and thats why I am trying to Flip the byte and getting it to work.
    To be honest, I am not sure what you are really trying to do. What is the bigger picture of the problem that you are trying to solve?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #22
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    If you believe that "12594678901234567890testnode123456" is an integer, there's not a lot we can do for you.

    If you want the integer to have the value of (the first part of) the contents of that string, you need to use strtol to convert the string to a long. The assignment, as you have it, will use the value of "12594678901234567890testnode123456" (note that the value of the string is its address, unrelated to the contents of the string itself) as the value for your variable.
    Last edited by tabstop; 06-01-2009 at 10:57 AM. Reason: pun'ctuat'ion

  8. #23
    Registered User
    Join Date
    May 2009
    Posts
    13
    I think problem here is nobody really looking at code of FlipByte.

    What it is trying to do and as mentioned it dumps the code when changing the value of it.

    TempBytePtr[0] = TempBytePtr[1];

    The value of subscript 0 = 1
    The value of subscript 1 = 2

  9. #24
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by shaileshk View Post
    I have checked the value of Type using gdb as debugger on core file and the value of Type is "12594678901234567890testnode123456"
    This may be true in your program, it does not mean that is a good thing, or that it will not cause an error.

    You can (by mistake or on purpose) put an incorrect or "dangerous" value into a variable (such as one that is too large for the type), and the debugger will show you this value. It is *your job* as the programmer using the tool to recognize that "12594678901234567890testnode123456" DOES NOT BELONG in an unsigned 16-bit integer. Hopefully you know how that value got there...
    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

  10. #25
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by shaileshk View Post
    What it is trying to do and as mentioned it dumps the code when changing the value of it.
    Yes, very possibly because you put way too big a value into "Type" to start with, which is guaranteed to cause an overflow. The consequences of that may not be immediately apparent, but it probably will not take long for a segfault to occur, during what ever set of operations happens to occur next.
    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

  11. #26
    Registered User
    Join Date
    May 2009
    Posts
    13
    I havent put this value purposefully but when casting from string to structure member it got inside directly... is there any way to stop this happening...

  12. #27
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by shaileshk
    I havent put this value purposefully but when casting from string to structure member it got inside directly... is there any way to stop this happening...
    Yes. Do not cast "from string to structure member". Maybe that is a correct solution, but without the bigger picture of what you are trying to solve we may well be helping you on a wild goose chase.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #28
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by shaileshk View Post
    I havent put this value purposefully but when casting from string to structure member it got inside directly... is there any way to stop this happening...
    Going back to your Original Post:

    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;
    }
    Understand that the value of pstruct->b WILL NOT be 1234[...], although you may think that.

    On my system, an int is four bytes. A C string is a series of one byte characters. So you have cast a string into MyStruct, the first ten bytes of which is a char array. Then you have an int, four bytes, which those four bytes will be the characters '1','2','3','4'. According to the ASCII table, the *integer* value of those four characters are

    1 = 49
    2 = 50
    3 = 51
    4 = 52

    These are signed values, so the bits set for each byte are
    0011 0001
    0011 0010
    0011 0011
    0011 0100
    On a little endian machine, the first byte is the least significant, so it has the same value, 49. The next byte's bits represent

    12686 64384 32192 16096 4048 1024 512 256

    so 0011 0010 will add 32192+16096+512 to the total, so we now have 48,849 and the most significant bytes are still to come.

    Do you see what happened?
    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

  14. #29
    Registered User
    Join Date
    May 2009
    Posts
    13
    Yes, but then if its '1234' then why it is falling at the point when its fliping the value i.e. TempBytePtr[0] = TempBytePtr[1];

    And when I comment this piece of code it do work correctly so i have doubt that the fliping is not working due to some restrictions.

  15. #30
    Registered User
    Join Date
    Aug 2006
    Posts
    100
    Quote Originally Posted by shaileshk View Post
    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.
    You simply CANNOT do that. You have an array of characters. Period.
    You cannot just "Cast" a string to an integer, and get the same representation.

    I think I see what you are doing, you are trying to write (or read) a raw structure of data. The first 10 characters will end up as a NON-null terminated string in struct.a (assuming no padding). The next 4 bytes (32 bit system) or 2 bytes (16 bit system) will be interpreted as a raw representation of an integer. Then you will get 10 more characters, again, non null terminated.

    Do you understand the fundamental differences between string representations of a number and binary representations?

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