One struct pointer question

Discussion in 'C Programming' started by QQ, May 7, 2005.

  1. QQ

    QQ Guest

    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!
     
    QQ, May 7, 2005
    #1
    1. Advertising

  2. QQ

    Guest

    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.
     
    , May 7, 2005
    #2
    1. Advertising

  3. In article <>,
    "QQ" <> 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.
     
    Christian Bau, May 7, 2005
    #3
  4. Emmanuel Delahaye, May 8, 2005
    #4
  5. On 7 May 2005 14:34:11 -0700, "QQ" <> 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>>
     
    Barry Schwarz, May 21, 2005
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Chris Fogelklou
    Replies:
    36
    Views:
    1,423
    Chris Fogelklou
    Apr 20, 2004
  2. Fernan Bolando

    Pointer to struct or Struct parameters

    Fernan Bolando, Aug 7, 2004, in forum: C Programming
    Replies:
    6
    Views:
    395
    Fernan Bolando
    Aug 8, 2004
  3. beetle
    Replies:
    2
    Views:
    943
    beetle
    Jan 25, 2005
  4. Zero
    Replies:
    16
    Views:
    674
    Barry Schwarz
    Nov 19, 2005
  5. aleksa

    Struct pointer vs. struct array pointer

    aleksa, Feb 20, 2013, in forum: C Programming
    Replies:
    16
    Views:
    494
    Shao Miller
    Feb 20, 2013
Loading...

Share This Page