C++ Problem with accessing elements of a character

  • Context: C/C++ 
  • Thread starter Thread starter AppleBite
  • Start date Start date
  • Tags Tags
    C++ Elements
Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
7 replies · 3K views
AppleBite
Messages
54
Reaction score
0
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)
{
fx=buff[i+2];
i=i+1;
}


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

count<<buff;


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;

count<<fx<<endl;


Anyone know what I'm doing wrong? Why am I getting a series of correct numbers from "buff", but strange data from "fx"?
 
Physics news on Phys.org
You need to initialize i to zero before doing the while loop, or you could use a for loop:

Code:
    for(i = 0; i < 7; i += 1)
    {
        fx[i] = buff[i+2];
    }
 
I'm still having a few problems though.

The output to "buff" is:
<error>,xxxxxx,xxxxxx,xxxxxx,xxxxxx,xxxxxx,xxxxxx<cr><lf>

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,

buff[i+2];

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
 
AppleBite said:
I'm still having a few problems though.

The output to "buff" is:
<error>,xxxxxx,xxxxxx,xxxxxx,xxxxxx,xxxxxx,xxxxxx<cr><lf>

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,

buff[i+2];

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

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:
char ch;
means that ch can hold one character, which may or may not be one byte.

This type of declaration
Code:
char name[10];
means that name can hold 10 characters.
 
Applebite:

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.
 
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:
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 */
 
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.

Also:

Adyssa said:
When you work with C strings you should:

- 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.
[/code]

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.