Lost with Arrays

Dear all,
I'm struggling with arrays and pass data from one array to the other...
below you can find a simple sketch where there are:

  • one array of integer
  • one array of "String" (ok, is not and array but a pointer)
    I wold like fo assing to the third array some numbers of array 1 and to the fourth array some string of the array2.
    I do not why but it works fine with String but integer has some problem:
int array1[] = { 10, 20, 30, 40, 50, 60, 70, 80, 90, 100 };
char *array1_names[] = { "A1", "B2", "C3", "D4", "E5", "F6", "G7", "H8", "J9", "K10" };

int array2[4];
char *array2_names[4];

void setup() {
  Serial.begin(9600);
  delay(3000);
  // Check array 1
  Serial.println("array1");
  for (int i = 0; i <= 9; i++) {
    Serial.println(array1[i]);
    Serial.println(array1_names[i]);
  }

  array2[0] = array1[0];
  array2_names[0] = array1_names[0];
  array2[1] = array1[2];
  array2_names[1] = array1_names[2];
  array2[2] = array1[4];
  array2_names[2] = array1_names[4];
  array2[3] = array1[6];
  array2_names[3] = array1_names[6];
  array2[4] = array1[8];
  array2_names[4] = array1_names[8];
  array2[5] = array1[10];
  array2_names[5] = array1_names[10];
  delay(3000);
  Serial.println("array2");
  // Check array 2
  for (int t = 0; t <= 4; t++) {
    Serial.println(array2[t]);
    Serial.println(array2_names[t]);
  }
}
void loop() {
  // put your main code here, to run repeatedly:
}

The Serial output is:
array1
10
A1
20
B2
30
C3
40
D4
50
E5
60
F6
70
G7
80
H8
90
J9
100
K10
array2
336
A1
0
C3
50
E5
70
G7
-18171
J9
Ln 41, Col 1
Arduino Uno
on COM9
What's wrong with position 0 and position 4 of array2 ?

Thanks in advance
Kind regards

Salmec

char *array2_names[4];

This declares an array of 4 pointers from array2[0] to array2[3]

array2[4] = array1[8];

Where in the array will this value be stored ?

but your array smaller that this, is it not? hence the underfined output...

(that's 0 - 3 :wink: )

Dear Sherzaad,
yes, you are right:

  • array1 has 10 values (0-9)

  • array1_names has 10 values (0-9)

  • array2 has 5 values (0-4)

  • array2_names has 5 values (0-4)

unfortunately correcting the last assignation with 9 it leads to work result.
I do not understand why also the first location of array2 is wrong...

int array1[] = { 10, 20, 30, 40, 50, 60, 70, 80, 90, 100 };
char *array1_names[] = { "A1", "B2", "C3", "D4", "E5", "F6", "G7", "H8", "J9", "K10" };

int array2[4];
char *array2_names[4];

void setup() {
  Serial.begin(9600);
  delay(3000);
  array2[0] = array1[0];
  array2_names[0] = array1_names[0];
  array2[1] = array1[2];
  array2_names[1] = array1_names[2];
  array2[2] = array1[4];
  array2_names[2] = array1_names[4];
  array2[3] = array1[6];
  array2_names[3] = array1_names[6];
  array2[4] = array1[8];
  array2_names[4] = array1_names[8];
  array2[5] = array1[9];
  array2_names[5] = array1_names[9];
  delay(3000);
  Serial.println("array2");
  // Check array 2
  for (int t = 0; t <= 4; t++) {
    Serial.println(array2[t]);
    Serial.println(array2_names[t]);
  }
}

void loop() {
  // put your main code here, to run repeatedly:
}

the serial output is
array2
336 <- this is wrong
A1
339
C3
50
E5
70
G7
-18171<- this is wrong
J9
Ln 214, Col 1

Arduino Uno
on COM9
2

Dear UKHeliBob
thanks for your interest.
I'm not sure to understand your request, the aim is to store in the 4th position of array2 the value that is stored in the 8th position of array1.

why it works for some position and for others no?

Salmec

your accessing the 6th element of a 4 element array.

So don't

  array2[5] = array1[9];

a7

There is no array1_names[10], only 0 to 9.
array2_names[5] = array1_names[10];

You are still using array elements that do not exist e.g. array2[4].

When you do, undefined things will happen.

Bear in mind that array elements start at zero, not one.
So 10 elements in an array are numbered 0 to 9.

array2 has 4 positions but they are numbered 0 to 3, so you are trying to store something in the fifth position, not the fourth. Memory is allocated for the array when it is declared, so there is no memory allocated for array2[4]

thanks guys to open my eyes...
code correct is:

int array1[] = { 10, 20, 30, 40, 50, 60, 70, 80, 90, 100 };
char *array1_names[] = { "A1", "B2", "C3", "D4", "E5", "F6", "G7", "H8", "J9", "K10" };

int array2[5];
char *array2_names[5];

void setup() {
  Serial.begin(9600);
  delay(3000);
  // Check array 1
  Serial.println("array1");
  for (int i = 0; i <= 9; i++) {
    Serial.println(array1[i]);
    Serial.println(array1_names[i]);
  }

  array2[0] = array1[0];
  array2_names[0] = array1_names[0];
  array2[1] = array1[2];
  array2_names[1] = array1_names[2];
  array2[2] = array1[4];
  array2_names[2] = array1_names[4];
  array2[3] = array1[6];
  array2_names[3] = array1_names[6];
  array2[4] = array1[8];
  array2_names[4] = array1_names[8];
  delay(3000);
  Serial.println("array2");
  // Check array 2
  for (int t = 0; t <= 4; t++) {
    Serial.println(array2[t]);
    Serial.println(array2_names[t]);
  }
}

void loop() {
  // put your main code here, to run repeatedly:
}

Summarizing for other people help (and to be sure that I've fully understanded...)
in the declaration
int array2[5]; means that array 2 is an array of 5 positions
but array position start from 0 up to 4:
array2[0] is the first position
array2[4]; is the fifth position (the last one)

thanks again everybody

Kind Regards

Salmec

this does not exist. array2 indexes go from 0 to 3

EDIT: sorry you changed it along the way

side note, you should use a struct to get the things together, it will make your like easier as you can assign everything in one go.

struct Data {
  int value;
  const char * name;
};

const Data originalData[] = {
  {10, "A1"},
  {20, "B2"},
  {30, "C3"},
  {40, "D4"},
  {50, "E5"},
  {60, "F6"},
  {70, "G7"},
  {80, "H8"},
  {90, "J9"},
  {100, "K10"}
};
const byte originalDataCount = sizeof originalData / sizeof * originalData; // if you need the count

const byte extractedDataCount = 4;
Data extractedData[extractedDataCount];

void setup() {
  Serial.begin(115200);
  extractedData[0] = originalData[0];
  extractedData[1] = originalData[2];
  extractedData[2] = originalData[4];
  extractedData[3] = originalData[6];
  Serial.println("extractedData");
  for (byte t = 0; t < extractedDataCount; t++) {
    Serial.print(extractedData[t].value); 
    Serial.write('\t');
    Serial.println(extractedData[t].name);
  }
}

void loop() {}

you'll see in the serial monitor (set at 115200 bauds)

extractedData
10	A1
30	C3
50	E5
70	G7

note that you perform a copy, so you are not reusing the entries from the originalData array.
The extractedData array could hold pointers into the originalData array and this way you would have no duplication. that would go like this.

struct Data {
  int value;
  const char * name;
};

const Data originalData[] = {
  {10, "A1"},
  {20, "B2"},
  {30, "C3"},
  {40, "D4"},
  {50, "E5"},
  {60, "F6"},
  {70, "G7"},
  {80, "H8"},
  {90, "J9"},
  {100, "K10"}
};
const byte originalDataCount = sizeof originalData / sizeof * originalData; // if you need the count

const byte extractedDataCount = 4;
const Data* extractedData[extractedDataCount];

void setup() {
  Serial.begin(115200);
  extractedData[0] = &originalData[0];
  extractedData[1] = &originalData[2];
  extractedData[2] = &originalData[4];
  extractedData[3] = &originalData[6];
  Serial.println("extractedData");
  for (byte t = 0; t < extractedDataCount; t++) {
    Serial.print(extractedData[t]->value); 
    Serial.write('\t');
    Serial.println(extractedData[t]->name);
  }
}

void loop() {}
1 Like

Thanks J-M-LJackson
I definitevely use Struct. It is way more simply.

Kind regards

salmec

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.