Copy value (not reference) of array to 2D array

Greetings everyone!

I am working on a project and are having some trouble with a part of my code.

I am copying data from a char array (dataArray) into a 2D char array (measurementArray), but when I clear or change the data in dataArray, the data in measurementArray is also changed. I believe the problem is that the pointer in measurementArray is simply pointing to the same data as dataArray holds, but I can’t seem to fix it myself.

These are the interesting parts of my code:

char* measurementArray[MAX_MEASUREMENTS];
char dataArray[11];
// This was my first attempt, which had the problem above
measurementArray[noOfReadings] = dataArray;
// This was my first attempt, which had the problem above.
measurementArray[noOfReadings] = dataArray;
// My second attempt - transferring the characters one by one.
// But when printing measurementArray, I got odd characters, such as this: "ð".
for(int j = 0; j < 11; j++) {
    measurementArray[noOfReadings][j] = dataArray[j];
}
// Clearing dataArray to ensure no leftover data for when I put in new data.
if(dataArray[0] != 0x00) {
    for(int j = 0; j < 11; j++){
        dataArray[j] = 0x00, BYTE;
    }
}

I should also mention that dataArray can hold between 3 and 11 characters. The tests I’ve done so far has usually been with 5 chars.

I think that is the interesting parts. Let me know if you need anything else.

Thanks!
Neubert.

char* measurementArray[MAX_MEASUREMENTS];
char dataArray[11];

The measurementArray item is an array of pointers. The dataArray is an array of chars. They are not the same thing. I’d remove the * in the measurementArray declaration. Then, the copy one character at a time method will work.

You could also use memcpy to copy the whole array at once.

Yes, but I need to save several measurements, so I require the array of pointers.

Okay, I’ve played around with it a little and gotten further, but I’ve encountered a very strange error.

strcpy(measurementArray[noOfReadings], dataArray);

for(int j = 0; j < MAX_MEASUREMENT_LENGTH; j++){
    dataArray[j] = '\0'; // Clearing dataArray
}

Serial.println(measurementArray[noOfReadings]); // Print 1

noOfReadings++;

Serial.println(measurementArray[noOfReadings]); // Print 2

measurementArray[noOfReadings] = "#####";

Serial.println(measurementArray[noOfReadings]); // Print 3

Serial.println(measurementArray[noOfReadings-1]); // Print 4

The printout of the following is this:

12g12 // print 1
R // print 2
12g12 // print 3
12g12 // print 4

Print 1 should be what I copied from dataArray to measurementArray[noOfReadings] - it is.
Print 2 is correct as well as it is displaying memory that haven’t been used yet.
Print 3 however looks very strange to me. I just set it equal to “#####”, but it displays the same data print 1.
Print 4 looks ok too.

Am I doing something wrong? This looks completely weird to me.

Because measurementArray is an array of pointers,

measurementArray[noOfReadings] = "#####";

is the same as

 char *ptrToChars = "#####"

which isn't valid. A pointer point to a memory location. "#####" is not a memory location.

You could do:

measurementArray[noOfReadings] = strdup("#####");

This creates a memory location, stores "#####" at that location, and returns the location, which can then be stored in the array.

Code: char *ptrToChars = "#####"

which isn't valid.

The only bit that isn't valid about that is that it is missing a semicolon.

When the pointer is defined it can be pointed to a constant string. It can not be re-assigned to another constant string, though. I should have written that code snippet as:

char *ptrToChars;
ptrToChars = "#####";

When the pointer is defined it can be pointed to a constant string. It can not be re-assigned to another constant string, though

You're kidding, right?

I see that I was in fact doing something wrong. But strdup() solved all my problems! Many thanks!

strdup() solved all my problems

Won’t “strdup” stop on the first null character it encounters?
OK for strings, not good for integers.

I suppose so, but I'm only working on strings (char array). My array is sent measurements wirelessly and holds them temporarily until they can be transferred (again wirelessly) to the computer.

but I'm only working on strings (char array

Ah! Missed that - that's the problem with C, it's difficult to tell whether you're dealing with strings or simply an array of limited-range samples.