Thread: argggg!!! what's wrong with this structure???

  1. #1
    Commonboy
    Guest

    Angry argggg!!! what's wrong with this structure???

    #include <iostream.h>
    #include <string.h>

    typedef struct
    {
    char nadest[6];
    char nasource[6];
    int type;
    char data[160];
    int crc;
    }frame;


    int main()
    {
    int i;

    frame test;

    strcpy(test.nadest, "NA0000");
    strcpy(test.nasource, "NA0001");

    cout << test.nadest << endl;
    cout << test.nasource << endl;

    return 0;
    }


    Can someone tell me why when I print the results, it prints NA0000NA0001 for test.nadest, and NA0001 for test.nasource INSTEAD of NA0000 and NA0001 for test.nadest and test.nasource respectively??? For some reason, test.nadest gets the "NA0001" appended to the end of it which is NOT what I want. (I want NA0000 for that only)

    Please help me in how to fix this problem. Thanks.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >char nadest[6];
    >char nasource[6];
    You declare arrays of length 6, 0 to n - 1.

    >strcpy(test.nadest, "NA0000");
    >strcpy(test.nasource, "NA0001");
    You assign 6 characters to both arrays.

    >cout << test.nadest << endl;
    >cout << test.nasource << endl;
    Both arrays are printed as nul terminated strings, but both lack the nul character so cout keeps printing characters until it finds one. This is your problem, change the array declarations to 7.

    -Prelude
    My best code is written with the delete key.

  3. #3
    Commonboy
    Guest
    Ok, but what if I have to keep those strings at EXACTLY six bytes in length, non-NULL terminated. Is there any way to print it off then?

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Ok, but what if I have to keep those strings at EXACTLY six bytes in length, non-NULL terminated.
    You have to treat them as arrays, thus, print them as you would an array:
    Code:
    for ( int i = 0; i < 6; ++i )
      std::cout<< test.nadest[i];
    std::cout<<'\n';
    -Prelude
    My best code is written with the delete key.

  5. #5
    Commonboy
    Guest
    One more question. Is there any way to cut off the "NA0001" bytes off test.nadest (to make it a strlen of 6 bytes, non-null terminating) without affecting test.nasource? I still don't quite understand how that got appended to it.

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    test.nasource was probably allocated memory directly after test.nadest, when you went out of bounds for test.nadest you most likely moved into the memory owned by test.nasource. That doesn't mean test.nasource is a part of test.nadest, it means you goofed and went too far.

    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. passing structure arrays to functions?
    By bem82 in forum C Programming
    Replies: 3
    Last Post: 10-30-2006, 06:17 AM
  2. structure ...solution plz????
    By hegdeshashi in forum C Programming
    Replies: 4
    Last Post: 07-24-2006, 09:57 AM
  3. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 09:44 AM
  4. nested structure help
    By whistler in forum C Programming
    Replies: 1
    Last Post: 05-17-2002, 10:48 AM
  5. Serial Communications in C
    By ExDigit in forum Windows Programming
    Replies: 7
    Last Post: 01-09-2002, 10:52 AM