C++ Problem with accessing elements of a character

  • Context: C/C++ 
  • Thread starter Thread starter AppleBite
  • Start date Start date
  • Tags Tags
    C++ Elements
Click For Summary

Discussion Overview

The discussion revolves around a programming issue in C++ related to accessing and manipulating elements of a character array that stores data received from a force/torque sensor via Serial Communication. Participants explore how to correctly extract and store specific pieces of data from a string of sensor readings, addressing initialization, indexing, and string termination concerns.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant describes an issue with printing data from a character array, noting that while the full string from the buffer is correct, the extracted data appears as strange characters.
  • Another participant suggests initializing the index variable before the loop or using a for loop for clarity and correctness.
  • A later reply points out confusion between characters and character arrays, emphasizing the need for proper declarations and indexing.
  • One participant proposes using the strtok function to split the string into tokens to access data chunks between commas.
  • Concerns are raised about writing beyond the bounds of the character array and the importance of including a null terminator to avoid reading garbage data.
  • Another participant provides an example of proper string handling, including buffer size considerations and null termination.

Areas of Agreement / Disagreement

Participants generally agree on the importance of proper initialization, indexing, and string termination in C++ character arrays. However, there are differing opinions on the best approach to extract data from the buffer, with some suggesting different methods for handling the string data.

Contextual Notes

Limitations include potential misunderstandings about character versus character array definitions, as well as unresolved issues regarding the exact indexing needed to access specific data chunks from the buffer.

Who May Find This Useful

Readers interested in C++ programming, particularly those working with character arrays, string manipulation, and data extraction from sensor readings may find this discussion beneficial.

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"?
 
Technology 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];
    }
 
Doh!

Thanks! :)
 
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.
 

Similar threads

Replies
89
Views
7K
  • · Replies 25 ·
Replies
25
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 8 ·
Replies
8
Views
5K
  • · Replies 18 ·
Replies
18
Views
4K
  • · Replies 118 ·
4
Replies
118
Views
10K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 4 ·
Replies
4
Views
6K