1

I have a global array. I'd like to update the array while the program running. But, I've found that my program below doesn't work as I expected.

Please checkout the program source:

#define RespLength 10
char* response[] = {"0","0","0","0","0","0","0","0","0","0"};
int idxUpd  = 0;

void setup() {
    // put your setup code here, to run once:
    Serial.begin(115200);
    Serial.println("Start");
}

void loop() {
  // put your main code here, to run repeatedly:
    char assign[5];
    int wkt = random(10, 99);
    dtostrf(wkt, 4, 1, assign);

    response[idxUpd]  = assign;

    Serial.print("Iteration "+String(idxUpd)+" : ");
    for (int i=0;i<RespLength;i++){
        Serial.print(response[i]);Serial.print(",");
    }
    Serial.println();
    delay(500);

    idxUpd++;
    idxUpd = idxUpd >= 10 ? 0 : idxUpd;
}

Expected output: please concern to the patern

Iteration 0 : 12.2,0,0,0,0,0,0,0,0,0,
Iteration 1 : 12.2,23.7,0,0,0,0,0,0,0,0,
Iteration 2 : 12.2,23.7,65.3,0,0,0,0,0,0,0,
Iteration 3 : 12.2,23.7,65.3,0,0,0,0,0,0,0,
Iteration 4 : 12.2,23.7,65.3,15.9,0,0,0,0,0,0,
Iteration 5 : 12.2,23.7,65.3,15.9,10.1,0,0,0,0,0,
Iteration 6 : 12.2,23.7,65.3,15.9,10.1,11.6,0,0,0,0,
Iteration 7 : 12.2,23.7,65.3,15.9,10.1,11.6,78.9,0,0,0,
Iteration 8 : 12.2,23.7,65.3,15.9,10.1,11.6,78.9,98.2,0,0,
Iteration 9 : 12.2,23.7,65.3,15.9,10.1,11.6,78.9,98.2,68.6,90.1,

Current output:

Iteration 0 : 85.0,0,0,0,0,0,0,0,0,0,
Iteration 1 : 28.0,28.0,0,0,0,0,0,0,0,0,
Iteration 2 : 36.0,36.0,36.0,0,0,0,0,0,0,0,
Iteration 3 : 70.0,70.0,70.0,70.0,0,0,0,0,0,0,
Iteration 4 : 56.0,56.0,56.0,56.0,56.0,0,0,0,0,0,
Iteration 5 : 74.0,74.0,74.0,74.0,74.0,74.0,0,0,0,0,
Iteration 6 : 94.0,94.0,94.0,94.0,94.0,94.0,94.0,0,0,0,
Iteration 7 : 94.0,94.0,94.0,94.0,94.0,94.0,94.0,94.0,0,0,
Iteration 8 : 26.0,26.0,26.0,26.0,26.0,26.0,26.0,26.0,26.0,0,
Iteration 9 : 71.0,71.0,71.0,71.0,71.0,71.0,71.0,71.0,71.0,71.0,

From the result above, I can conclude that if I update the array value incrementally, it just overwrites the other element in the array.
So, How can I have my expected output?

2 Answers 2

3

You are using a char pointer (char*) instead of a char.

When you say;

response[idxUpd]  = assign;

you are assigning not the value but a pointer to that value. That points to assign for every element in response[]. Every response[] get the same pointer value, every response[] get the same value.

Use this:

#define RespLength 10
int response[] = {0,0,0,0,0,0,0,0,0,0};
int idxUpd  = 0;

void setup() {
    // put your setup code here, to run once:
    Serial.begin(115200);
    Serial.println("Start");
}

void loop() {
  // put your main code here, to run repeatedly:
    int wkt = random(10, 99);
    response[idxUpd]  = wkt;

    Serial.print("Iteration "); Serial.print(idxUpd); Serial.print(" : ");
    for (int i=0;i<RespLength;i++){
        Serial.print(response[i] );Serial.print(",");
    }
    Serial.println();
    delay(500);

    idxUpd++;
    idxUpd = idxUpd >= 10 ? 0 : idxUpd;
}

I don't understand why you are converting a int to a float. That value doesn't change, so I omitted the dtostr() call.

3
  • I'm sorry I didn't tell the story. So, I'm using char* because the response will be passed into Wire.write(). That function needs the parameter to be char*. I've tried to make the response as int or float, but it is not working well when passed to Wire.write() like this Wire.write(String(response[idxResp]).c)str()); Jul 20, 2017 at 5:03
  • @Oki. You can use Wire.write(value), where value is a byte. An unsigned byte can take values from 0-255. You can send int and float if you send them byte by byte. It's better keep you data as binary and convert it to string as needed. Study pointers and you will be a Master.
    – user31481
    Jul 20, 2017 at 9:22
  • I did read about pointers and etc some times ago. But my bad, I forget it because I'm not used to this programming style and language. I accept your suggestion. I changed the char* response[] to String response[] instead of float response[], I do this to minimize the conversion process. Jul 21, 2017 at 9:35
1

C/C++ Arrays don't work the way you are used from scripting languages. Your

char* response[] = {"0","0","0","0","0","0","0","0","0","0"};

allocates a consecutive space in memory with the lenght of 20 bytes. So you have 10 pointers that point to static memory.

In your loop you have a array of char assign[5] that gets assigned to every single of those pointers with response[idxUpd] = assign;. You copy pointers and not values here. So the only value you really have is that in your assign variable. So as you update the pointes of the array, you get the results you see.

2
  • I thought the pointer is char *response not char* response :D. Now I know why sizeof(response) returns 20. Jul 21, 2017 at 9:38
  • @Oki. A pointer in Arduino takes 2 bytes. You have a 10 elements (pointers) array, then 20 bytes. sizeof gives you the size in bytes of the argument.
    – user31481
    Jul 21, 2017 at 9:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.