C++ Problem with accessing elements of a character

In summary, the conversation discusses an issue with sampling data from a force/torque sensor using Serial Com. The code being used is mentioned and the desired goal is to store pieces of data separately. Suggestions are given to use either a for loop or the strtok function to split the string into tokens. The importance of declaring character arrays and using the correct indices is emphasized to prevent errors in reading the data.
  • #1
AppleBite
54
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:

cout<<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;

cout<<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
  • #2
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];
    }
 
  • #3
Doh!

Thanks! :)
 
  • #4
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
 
  • #5
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.
 
  • #6
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.
 
  • #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:
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 */
 
  • #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.

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.
 

FAQ: C++ Problem with accessing elements of a character

What is meant by "accessing elements" in C++?

Accessing elements in C++ refers to retrieving or modifying specific characters within a string or array of characters. This can be done using indexing or pointer arithmetic.

How do I access a specific character in a string or array in C++?

In C++, strings and arrays use zero-based indexing, meaning the first character is at index 0. To access a specific character, you can use square brackets and the index of the character you want to access. For example, if the string is "hello", to access the "e" you would use str[1].

What is the difference between accessing a character using indexing versus pointers in C++?

Indexing involves using the square bracket notation to access a specific character in a string or array. Pointers, on the other hand, refer to the address of a character in memory. To access a character using pointers, you would need to dereference the pointer using the * operator.

How can I avoid errors when accessing elements in C++?

To avoid errors when accessing elements in C++, make sure you are using valid indexes and pointers. For example, trying to access a character at an index outside of the string's length will result in an error. Additionally, make sure the string or array is not empty before trying to access any elements.

Can I modify characters in a string or array in C++?

Yes, characters in a string or array can be modified in C++. As long as the string is not a constant, you can use the assignment operator (=) to change the value of a specific character at a given index.

Similar threads

Replies
5
Views
2K
Replies
8
Views
2K
Replies
8
Views
2K
Replies
89
Views
5K
Replies
1
Views
2K
Replies
118
Views
7K
Replies
25
Views
2K
Replies
22
Views
2K
Back
Top