Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

C++ Problem with accessing elements of a character

  1. Jan 17, 2013 #1
    Hey guys,

    I'm having trouble with sampling data from a force/torque sensor. I'm using Serial Com to receive the readings from the sensor, which is stored in a character of with 45 individual elements. What I'm trying to do is to pick out pieces of these data and store them separately. I'm using the code:

    char * buff=serial.receive(45); // receives a string of data
    char fx[6]; // Character to store the fx-readings

    int i;
    while (i<7)

    Now, if I print the full string from the character "buff", I get the full readings of a series of numbers representing sensor readings:


    However, if I try to print the data stored in fx, which should be a small part of the data from buff, I get a combination of letters and symbols;


    Anyone know what I'm doing wrong? Why am I getting a series of correct numbers from "buff", but strange data from "fx"?
  2. jcsd
  3. Jan 17, 2013 #2


    User Avatar
    Homework Helper

    You need to initialize i to zero before doing the while loop, or you could use a for loop:

    Code (Text):

        for(i = 0; i < 7; i += 1)
            fx[i] = buff[i+2];
  4. Jan 17, 2013 #3

    Thanks! :)
  5. Jan 17, 2013 #4
    I'm still having a few problems though.

    The output to "buff" is:

    Now, what I'm trying to do is the store each of the "chunks" of data between the commas into separate chars. However, using the code above,


    does not seem to correspond to the first x after the first comma. Does anyone know how to access each section of x's, as when using the method above the correspondance between "i+" and the elements seem rather strange.

    Thanks guys
  6. Jan 17, 2013 #5


    Staff: Mentor

    One possibility is to use the strtok standard library function (http://www.cplusplus.com/reference/cstring/strtok/) to split your string into tokens.

    BTW, some of what you wrote threw me off at first, because you seem to be confused about the difference between a character and a character array. In your thread title you talk about accessing the elements of a character, and above, you talk about storing chunks of data into separate characters.

    A declaration such as
    Code (Text):
    char ch;
    means that ch can hold one character, which may or may not be one byte.

    This type of declaration
    Code (Text):
    char name[10];
    means that name can hold 10 characters.
  7. Jan 18, 2013 #6


    User Avatar
    Science Advisor
    Gold Member


    Note you have declared the array fx to have 6 characters in it, but you are writing 7 characters to it. The first entry is fx[0] not fx[1]. Also if the last character you write isn't a "NUL" (zero) then software trying to read it as a string won't know where to stop and may carry on reading memory beyond the end of the array.
  8. Jan 19, 2013 #7
    When you work with C strings you should:

    - declare your string buffer to be 1 character larger than the length of string you wish to store
    - as mentioned above, the first index is 0 and the last is length-1, so a buffer of size 100 has slots 0-98 available for characters and you must leave the last slot, 99, for '\0' (the null character) which terminates the string.
    - be very careful about how large you make your buffers and how you index your arrays. If you declare char buff[100], you cannot index buff[100]. the last slot is buff[99] (null character) and the second last slot buff[98] has the last readable character.

    Here's an example:

    Code (Text):
    char buff[100]; /* room for 99 chars + null terminator from index 0-99*/
    char fx[7]; /* room for 6 chars + null terminator from index 0-6*/
    int i = 0; /* initialize i */

    for (i = 0; i < 6; i++)
        fx[i] = buff[i + 2]; /* copy char from buff to fx*/

    fx[6] = '\0'; /* terminate the string! */

    cout << fx << endl /* print fx as a string */

  9. Jan 22, 2013 #8
    Thanks guys. Indeed, I was quite confused about characters and character arrays, but I think I've sort of got the hang of it now.


    This solved my issues with reading the correct data, as without the 'NULL' the software just kept on storing garbage into my fx, fy and fz arrays.
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook