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
I have checked the value of Type using gdb as debugger on core file and the value of Type is "12594678901234567890testnode123456"
Ta,
Shail
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?Originally Posted by shaileshk
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
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
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
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
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
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.Originally Posted by shaileshk
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Going back to your Original Post:
Understand that the value of pstruct->b WILL NOT be 1234[...], although you may think that.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; }
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
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.
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?