I'm trying to find a way to assign a specific column to a specific value in a multidimensional array
Per below, I'm aware of how to assign it manually and through a for loop.
Is there an easier way to go about doing it? thanks
#include < stdio.h >
double Test1[4][5];
double a0, a1, a2, a3;
int main() {
//Assigning one column in a specific row manually
Test1[1][1] = 1;
a0 = Test1[0][1];
a1 = Test1[1][1];
a2 = Test1[2][1];
a3 = Test1[3][1];
printf("a0 %f \r\n", a0);
printf("a1 %f \r\n", a1);
printf("a2 %f \r\n", a2);
printf("a3 %f \r\n", a3);
int row = sizeof(Test1) / sizeof(Test1[0]);
printf("rows %d \r\n", row);
int column = sizeof(Test1[0]) / sizeof(Test1[0][0]);
printf("cols %d \r\n", column);
int L;
double a;
//Assigning one column in all rows to one
for (L = 0; L < row; L = L + 1) {
Test1[L][1] = 1;
}
a0 = Test1[0][1];
a1 = Test1[1][1];
a2 = Test1[2][1];
a3 = Test1[3][1];
printf("a0 %f \r\n", a0);
printf("a1 %f \r\n", a1);
printf("a2 %f \r\n", a2);
printf("a3 %f \r\n", a3);
return 0;
}
There is no standard function that sets a column of a 2D array. In C, multidimensional arrays are a bit of an illusion; they compile into 1D arrays. Here is some code to demonstrate that the values unwind into a 1D array:
#include <stdio.h>
int main(){
int test[10][2] = {0};
//point to the 1st element
int * p1 = &test[0][0];
//20th position is the 9th row, 2nd column
p1[19] = 5;
//9th element is the 5th row, 1st column
int * p2 = p1 + 8;
*p2 = 4;
printf("Value set to 5: %d\n",test[9][1]);
printf("Value set to 4: %d\n",test[4][0]);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.