string and struct problem

Discussion in 'C++' started by vishnu, Apr 22, 2005.

  1. vishnu

    vishnu Guest

    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
    vishnu, Apr 22, 2005
    #1
    1. Advertising

  2. vishnu

    Tim Love Guest

    "vishnu" <> 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.
    Tim Love, Apr 22, 2005
    #2
    1. Advertising

  3. vishnu

    Ron Natalie Guest

    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.
    Ron Natalie, Apr 22, 2005
    #3
  4. 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
    Fred L. Kleinschmidt, Apr 22, 2005
    #4
    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. RA Scheltema
    Replies:
    3
    Views:
    398
    RA Scheltema
    Jan 6, 2004
  2. Chris Fogelklou
    Replies:
    36
    Views:
    1,373
    Chris Fogelklou
    Apr 20, 2004
  3. Replies:
    3
    Views:
    1,086
  4. Tzury Bar Yochay
    Replies:
    4
    Views:
    570
    John Machin
    Sep 21, 2008
  5. Noob
    Replies:
    25
    Views:
    1,471
    Nick Keighley
    Dec 9, 2009
Loading...

Share This Page