1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
|
given a 1d array
unsigned char 1d[] = {
0x01, 0x02, 0x03, 0x04, 0x05,
0x06, 0x07, 0x08, 0x09, 0x00,
0x01, 0x02, 0x03, 0x04, 0x05,
0x06, 0x07, 0x08, 0x09, 0x00
};
//I am trying to convert that 1d array to a 2d array. I printf width and height.
width = 5, height = 4.
for (int i = 0; i < height; i++){
for (int j = 0; j < width; j++){
printf("%d",1d[icounter]);
iarray[i][j] = 1d[icounter];
icounter++;}}
//result: 12345678901324567890 same thing if I do print iarray[i][j].
for (int i = 0; i < height; i++){
for (int j = 0; j < width; j++){
printf("%d",iarray[i][j]);}}
//result: 123466789112345667890. Why? why 6 and 1 appear twice. To help you guys visualize it I'll have one more for loop exactly the same as the above. but with some space.
for (int i = 0; i < height; i++){printf("\n");
for (int j = 0; j < width; j++){
printf("%d",iarray[i][j]);}}
/*
result:
1 2 3 4 6
6 7 8 9 1
1 2 3 4 6
6 7 8 9 0 */
/*
I want my result to be
1 2 3 4 5
6 7 8 9 0
1 2 3 4 5
6 7 8 9 0 */
//lastly I try printing the incidences to see what's going on.
for (int i = 0; i < height; i++){
for (int j = 0; j < width; j++){
printf("(%d",i);
printf(",%d",j);
printf(")");
printf("%d\n",iarray[i][j]);}}
/*result:
(0,0)1
(0,1)2
(0,2)3
(0,3)4
(0,4)6
(1,0)6
(1,1)7
(1,2)8
(1,3)9
(1,4)1
(2,0)1
(2,1)2
(2,2)3
(2,3)4
(2,4)6
(3,0)6
(3,1)7
(3,2)8
(3,3)9
(3,4)0
*/
Now i'm confused.
|