string and struct problem

V

vishnu

hi friends here is small program :

int main()
{
struct rec
{
unsigned short rno;

};
union
{
unsigned char b[2];
struct rec record1;
};
strcpy((char *)b,"66");
printf("from record1 = %d\n",record1.rno);
return 0;

}

am getting output as :
from record1 = 13878

why am not getting 66
what is going wrong here
 
T

Tim Love

vishnu said:
hi friends here is small program :
int main()
{
struct rec
{
unsigned short rno;
};
union
{
unsigned char b[2];
struct rec record1;
};
strcpy((char *)b,"66");
printf("from record1 = %d\n",record1.rno);
return 0;

am getting output as :
from record1 = 13878
why am not getting 66
what is going wrong here
I'm unsure why you'd want to do this, and b[2] should be at
least b[3]. Putting a string representation of a number into
memory then reading it as if it were an int isn't going to
make it an int, just as writing something in German and getting
an English monoglot to read it isn't going to translate it into English.
 
R

Ron Natalie

vishnu said:
hi friends here is small program :

You're lucky you're getting anything. In addition to your
fundamental misunderstanding of the relationship between characters
and numbers, your program exhibits undefined behavior on a number
of accounts.

strcpy((char *)b,"66");

This copies THREE characters. The string "66" are the two '6'
characters and a zero byte that terminates the string. You are
copying it into an array that's only two characters.
printf("from record1 = %d\n",record1.rno);

Your lucky this did anything. There's no guarantee you can read
out of a union member that is different than the one you stored into.

Further, no CONVERSIONS occur when accessing unions this way (not
that one would help here).
am getting output as :
from record1 = 13878

why am not getting 66
what is going wrong here

OK, lets see what likely happened. You stored two bytes in the array.
Each byte had the value '6' which in hexadecimal is 0x36. If you did:
printf("%x\n", '6');
you'll see that.

So it happens your short consists of a 0x36 in the low 8 bits and a
0x36 in the high 8 bits or 0x3636 or converted to decimal 13878.

If you want to convert a character array containing a representation
of a number you can use the strtol (or one of the related)
functions.
 
F

Fred L. Kleinschmidt

vishnu said:
hi friends here is small program :

int main()
{
struct rec
{
unsigned short rno;

};
union
{
unsigned char b[2];
struct rec record1;
};
strcpy((char *)b,"66");
Crash! you just overwrote memory. You are copying 3 characters (don't
forget the trailing '\0') into an array of only two bytes.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top