One struct pointer question

Q

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!
 
C

cltools

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

Christian Bau

"QQ said:
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.
 
B

Barry Schwarz

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

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,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top