Adding two arrays?

In the Arduino IDE, I would like to add the contents of two existing arrays, for example:

#define L0 { {0, 0, 0, 0}, {0, 0, 0, 1}, {0, 0, 0, 0} } #define L1 { {0, 0, 0, 1}, {0, 0, 0, 0}, {0, 0, 0, 0} } 

should become

  int myarray[3][4] = { {0, 0, 0, 1}, {0, 0, 0, 1}, {0, 0, 0, 0} } 

How can i do this?

Thanks!

+4
source share
2 answers

This:

 const int a[3][4] = { {0, 0, 0, 0}, {0, 0, 0, 1}, {0, 0, 0, 0} }; const int b[3][4] = { {0, 0, 0, 1}, {0, 0, 0, 0}, {0, 0, 0, 0} }; int c[3][4]; const int* pa = &a[0][0]; const int* pb = &b[0][0]; int* pc = &c[0][0]; for(int i = 0; i < 3 * 4; ++i) { *(pc + i) = *(pa + i) + *(pb + i); } 
+2
source

I think you are confused about how to access the arrays L0 and L1 , as they are defined as macros. Just assign them to arrays, as the preprocessor will simply replace them:

 int l[][4]=L0; int m[][4]=L1; 

The preprocessor will replace L0 and L1 with its values, and the compiler will see them only as:

 int l[][4]={ {0, 0, 0, 0}, {0, 0, 0, 1}, {0, 2, 0, 0} }; int m[][4]={ {0, 0, 0, 5}, {0, 0, 0, 6}, {0, 0, 7, 0} }; 

Now you can use l and m to access the elements of the array. It should be easy enough to add two arrays from here :)

+2
source

Source: https://habr.com/ru/post/1439465/


All Articles