473,379 Members | 1,544 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

string and struct problem

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
Jul 23 '05 #1
3 1544
"vishnu" <vi*************@in.bosch.com> writes:
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.

Jul 23 '05 #2
vishnu wrote:
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.
Jul 23 '05 #3


vishnu wrote:

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.
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


--
Fred L. Kleinschmidt
Boeing Associate Technical Fellow
Technical Architect, Common User Interface Services
M/S 2R-94 (206)544-5225
Jul 23 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

26
by: Kai Jaensch | last post by:
Hello, i am an newbie and i have to to solve this problem as fast as i can. But at this time i donīt have a lot of success. Can anybody help me (and understand my english :-))? I have a...
4
by: vertigo | last post by:
Hello I have: struct packet{ byte type; /*my byte is unsigned char*/ int len; } struct packet *p = new struct packet; p->type=10; p->len=0;
5
by: Tomas | last post by:
Hi NG. I have a problem with strings. I have defined this class: class wort_cl { int haeufigkeit; int length; string wort;
3
by: Leo Nunez | last post by:
Hello! I need copy from structure "A" to "B" that contains "strings" in a one line code. Me problem like this : typedef struct tHeader{ char field1; char field2; char field3;
29
by: zoltan | last post by:
Hi, The scenario is like this : struct ns_rr { const u_char* rdata; }; The rdata field contains some fields such as :
1
by: Zhiyao | last post by:
I want to write a program which can read strings, one per line, from standard input, and the program builds a list of strings then prints it. The code is listed below, when I compile it, there is no...
13
by: coosa | last post by:
Dear all, Using the conio implementation i wanted to create a dynamic string, whereby its size would be determined after each keyboard hit; in other words, i don't want to ask the user to...
111
by: Tonio Cartonio | last post by:
I have to read characters from stdin and save them in a string. The problem is that I don't know how much characters will be read. Francesco -- ------------------------------------- ...
8
by: pabl0 | last post by:
i have an array of strings that need sorting,(not so much an array of strings but a struct that con tains an array of strings) technically the strings are already sorted. an example is in order. ...
3
by: jacob navia | last post by:
Abstract: Continuing the discussion about abstract data types, in this discussion group, a string collection data type is presented, patterned after the collection in C# and similar languages...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.