473,396 Members | 1,996 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,396 software developers and data experts.

One struct pointer question

QQ
Here is my program
the head is
typedef struct struct_CN
{
unsigned char magicA;
unsigned char magicB;
unsigned short msgLen;
} CN;
typedef struct struct_CcDev
{
CN Header;
unsigned short action;
} CcDev;

CcDev CcDev_packet;

Evaluate_CN(CN *CN_p)
{

strcpy(&CN_p->magicA,"A");
printf("magicA is %s\n", &CN_p->magicA);
strcpy(&CN_p->magicB,"H");
printf("magicB is %s\n", &CN_p->magicB);
CN_p->msgLen = 1234;
printf("msgLen is %d\n", &CN_p->msgLen);
}
void Evaluate_CcDevRegMsg()
{
Evaluate_CN(&CcDev_packet.msgHeader);
printf("CcDev_packet.CN.magicA = %s\n",&CcDev_packet.Header.magicA);
printf("CcDev_packet.CN.magicB = %s\n",&CcDev_packet.Header.magicB);
printf("CcDev_packet.CN.msgLen = %d\n",&CcDev_packet.Header.msgLen);
}
When I use gcc to compile
gcc file.c

I got the output as
magicA is A
magicB is H
msgLen = 1234
CcDev_packet.CN.magicA = AH?
CcDev_packet.CN.magicB = H?
CcDev_packet.CN.msgLen = 134519298
Is there anything wrong with it?

Thanks a lot!

Nov 14 '05 #1
4 1259

QQ a écrit :
Here is my program
the head is
typedef struct struct_CN
{
unsigned char magicA;
unsigned char magicB;
unsigned short msgLen;
} CN;
typedef struct struct_CcDev
{
CN Header;
unsigned short action;
} CcDev;

CcDev CcDev_packet;

Evaluate_CN(CN *CN_p)
{

strcpy(&CN_p->magicA,"A");
printf("magicA is %s\n", &CN_p->magicA);
strcpy(&CN_p->magicB,"H");
printf("magicB is %s\n", &CN_p->magicB);
CN_p->msgLen = 1234;
printf("msgLen is %d\n", &CN_p->msgLen);
}
void Evaluate_CcDevRegMsg()
{
Evaluate_CN(&CcDev_packet.msgHeader);
printf("CcDev_packet.CN.magicA = %s\n",&CcDev_packet.Header.magicA); printf("CcDev_packet.CN.magicB = %s\n",&CcDev_packet.Header.magicB); printf("CcDev_packet.CN.msgLen = %d\n",&CcDev_packet.Header.msgLen); }
When I use gcc to compile
gcc file.c

I got the output as
magicA is A
magicB is H
msgLen = 1234
CcDev_packet.CN.magicA = AH?
CcDev_packet.CN.magicB = H?
CcDev_packet.CN.msgLen = 134519298
Is there anything wrong with it?

Thanks a lot!

You can't use strcpy to assign char.
You can't use printf %s to display char, use %c instead

Strcpy will assign two bytes, first with 'A', second with nul ('\0')

strcpy(&CN_p->magicA,"A");
should be replaced by
CN_p->magicA = 'A';

But maybe you *really* want to use strings, then you should declare
typedef struct struct_CN {
unsigned char *magicA;
unsigned char *magicB;
unsigned short *msgLen;
} CN;

But then, you need to allocate space prior to store anything.

Nov 14 '05 #2
In article <11*********************@z14g2000cwz.googlegroups. com>,
"QQ" <ju****@yahoo.com> wrote:
Here is my program
the head is
typedef struct struct_CN
{
unsigned char magicA;
unsigned char magicB;
unsigned short msgLen;
} CN;
typedef struct struct_CcDev
{
CN Header;
unsigned short action;
} CcDev;

CcDev CcDev_packet;

Evaluate_CN(CN *CN_p)
{

strcpy(&CN_p->magicA,"A");
printf("magicA is %s\n", &CN_p->magicA);
strcpy(&CN_p->magicB,"H");
printf("magicB is %s\n", &CN_p->magicB);
CN_p->msgLen = 1234;
printf("msgLen is %d\n", &CN_p->msgLen);
What exactly is &CN_p->msgLen? And what exactly does the %d format print?
}
void Evaluate_CcDevRegMsg()
{
Evaluate_CN(&CcDev_packet.msgHeader);
printf("CcDev_packet.CN.magicA = %s\n",&CcDev_packet.Header.magicA);
printf("CcDev_packet.CN.magicB = %s\n",&CcDev_packet.Header.magicB);
printf("CcDev_packet.CN.msgLen = %d\n",&CcDev_packet.Header.msgLen);
}
When I use gcc to compile
gcc file.c

I got the output as
magicA is A
magicB is H
msgLen = 1234 *************

Unlikely that the code above printed 1234. I guess the relevant line of
code was
printf("msgLen is %d\n", CN_p->msgLen);
Can you see the difference?
CcDev_packet.CN.magicA = AH?
CcDev_packet.CN.magicB = H?
CcDev_packet.CN.msgLen = 134519298
Is there anything wrong with it?


Yes. What is the difference between a char and a C string? What does
strcpy do? Most definitely not what you think it does.
Nov 14 '05 #3
QQ wrote on 07/05/05 :
Here is my program

<...>

See my answer to your previous post.

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

..sig under repair

Nov 14 '05 #4
On 7 May 2005 14:34:11 -0700, "QQ" <ju****@yahoo.com> wrote:
Here is my program
the head is
typedef struct struct_CN
{
unsigned char magicA;
unsigned char magicB;
unsigned short msgLen;
} CN;
typedef struct struct_CcDev
{
CN Header;
unsigned short action;
} CcDev;

CcDev CcDev_packet;

Evaluate_CN(CN *CN_p)
{

strcpy(&CN_p->magicA,"A");
magicA has room for a single character. You are attempting to copy
two characters into it. The only reason this does not lead to
undefined behavior is that the next byte in memory is guaranteed to
belong to your structure.
printf("magicA is %s\n", &CN_p->magicA);
strcpy(&CN_p->magicB,"H");
Same for magicB.
printf("magicB is %s\n", &CN_p->magicB);
CN_p->msgLen = 1234;
printf("msgLen is %d\n", &CN_p->msgLen);
}
void Evaluate_CcDevRegMsg()
{
Evaluate_CN(&CcDev_packet.msgHeader);
printf("CcDev_packet.CN.magicA = %s\n",&CcDev_packet.Header.magicA);
magicA need not be the address of a valid string. If we assume that
the structure has no padding (reasonable if sizeof(short) is 2), then:
The first strcpy put the 'A' in magicA and a '\0' in magicB.
The second put the 'H' in magicB and the '\0' in the first byte of
msglen.
The integer assignment put 0x04d2 into msglen (in either order).

Where is the '\0' that will terminate the string that starts with 'A'?
printf("CcDev_packet.CN.magicB = %s\n",&CcDev_packet.Header.magicB);
Ditto.
printf("CcDev_packet.CN.msgLen = %d\n",&CcDev_packet.Header.msgLen);
%d requires an int. You are passing it an int*. This leads to
undefined behavior. You probably did not intend to have the & there.
}
When I use gcc to compile
gcc file.c

I got the output as
magicA is A
magicB is H
msgLen = 1234
CcDev_packet.CN.magicA = AH?
CcDev_packet.CN.magicB = H?
CcDev_packet.CN.msgLen = 134519298
Is there anything wrong with it?
Of course.

Thanks a lot!


<<Remove the del for email>>
Nov 14 '05 #5

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

Similar topics

3
by: Emanuele Blanco | last post by:
Hi there, I just compiled a program that uses linked lists (needed it as an homework for my Programming course at University). It works flawlessly, even if I notice a thing. Here's my linked...
20
by: fix | last post by:
Hi all, I feel unclear about what my code is doing, although it works but I am not sure if there is any possible bug, please help me to verify it. This is a trie node (just similar to tree nodes)...
67
by: S.Tobias | last post by:
I would like to check if I understand the following excerpt correctly: 6.2.5#26 (Types): All pointers to structure types shall have the same representation and alignment requirements as each...
60
by: Mohd Hanafiah Abdullah | last post by:
Is the following code conformat to ANSI C? typedef struct { int a; int b; } doomdata; int main(void) { int x;
16
by: Zero | last post by:
Hi everybody! I have the following code: struct Infos { char Haarfarbe; int Groesse; }; struct Person
20
by: ma0001yu | last post by:
Hi, all. I feel confuse about below struct definition: typedef struct TAG { comments.... }; What my confusion is: is typedef extra??why we not just use
5
by: Yourko | last post by:
Hi there! I`me currently trying to write some simple programs in C. For one such program i created globals.h file. In that file i defined a structure of type _client, and a pointer of that type: ...
11
by: redefined.horizons | last post by:
First, I would thank all of those that took the time to answer my question about creating an array based on a numeric value stored in a variable. I realize after reading the responses and doing...
25
by: SRR | last post by:
Consider the following code: #include <stdio.h> #include <string.h> struct test{ char a; } funTest( void ); int main( void ) {
5
by: Chris Saunders | last post by:
Here is the C declaration of a struct from Windows: typedef struct _TXFS_READ_BACKUP_INFORMATION_OUT { union { // // Used to return the required buffer size if return code is...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.