problem with Array

This is a discussion on problem with Array within the C++ Programming forums, part of the General Programming Boards category; Hi I have declared a char array inside a local method char tempID[16] ; Then I read some characters from ...

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    4

    problem with Array

    Hi

    I have declared a char array inside a local method

    char tempID[16] ;

    Then I read some characters from the serial port, storing everything in the array "txt". char txt[40];

    I want to copy 16 characters (from the 5th to the 20th) from "txt to the tempID array, I have done it with a for loop:

    for(int i=0; i<sizeof(tempID); i++)
    {
    tempID[i] = txt[i+4];

    }


    The PROBLEM is that when I display the tempID array I get all the right characters PLUSS a final character that I have no idea where comes from. (Itīs a non-printable character, probalby CR or LF). Thought it was a problem with the for loop and appended some extra characters on the txt-string but the problem remains.

    Any suggestions would be appreciated.


    doneirik

  2. #2
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    c strings require a null terminator. Your loop does not provide this. You need to set the (i+1)st byte to 0 or '\0'
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  3. #3
    Registered User
    Join Date
    Dec 2004
    Posts
    4
    Quote Originally Posted by FillYourBrain
    c strings require a null terminator. Your loop does not provide this. You need to set the (i+1)st byte to 0 or '\0'
    I know....but though the "txt" array has been converted (automatically) to a string through the use of strcat, I need the tempID array as an clean array and not as a string. (cause I need to send it through the serial port again and it must comply with the protocol of the external hardware)


    doneirik

  4. #4
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856
    The only question, then, is how you are "displaying" tempID. If you call a function such as printf() it will treat tempID as an array of characters (a "string") because that is precisely what it is. Printing it this way means byte after byte will be printed until, but not after, a byte with a value of zero is encountered. If you display the contents via a loop similar to how you set it, you should see what you expect.

  5. #5
    Registered User
    Join Date
    Dec 2004
    Posts
    4
    Ok.
    I display it using this->textBox1->Text = tempID;

    But...A few lines later in my code I copy again the 16 characters in the tempID array into a new array

    char TagID[16];
    for(int i = 0; i<sizeof(tempID); i++)
    {
    TagID[i] = tempID[i];
    }


    displaying this array using the same methods works ok. The extra character is only appended in the first case.
    ...it is sort of annoying...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array problem
    By TomBoyRacer in forum C++ Programming
    Replies: 3
    Last Post: 04-08-2007, 12:35 PM
  2. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 05:15 AM
  3. Replies: 6
    Last Post: 02-15-2005, 11:20 PM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 10:54 AM
  5. Need desperate help with two dimensional array problem
    By webvigator2k in forum C++ Programming
    Replies: 4
    Last Post: 05-10-2003, 03:28 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21