Home > Net >  2d array address in c and select between arrays
2d array address in c and select between arrays

Time:11-25

I have some 2d double-type arrays in my c code and I want to select between them. The solution I have used so far is to create another 2d array and fill it with a for{ for{}}.

if (some condition)
     for()
       for()
         temp[i][j]=arr1[i][j];
else if (another condition)
     for()
       for()
         temp[i][j]=arr2[i][j];
 ...

Now I have tried the following code

double (*temp)[4][4];
double arr1[4][4],arr2[4][4];
if (some condition)
    temp = &arr1;
else if (another condition)
    temp = &arr2;

The problem with the latest code is that it only assigns the first row and other rows seemingly get incorrect addresses. What should I do to correct my code?

CodePudding user response:

You don't tell us how you use double (*temp)[4][4]; but it's a pointer or a 2d array, if increment it will point to the next 4x4 array. You need to deference the pointer first (*) then apply array subscripts ([][]) which require parenthesis ('(*...)[...][...]) to be evaluated in the correct order:

#include <stdio.h>

#define ROWS 4
#define COLS 4

int main(void) {
    double (*temp)[ROWS][COLS];
    double arr1[][COLS] = {
        { 1, 1, 1, 1 },
        { 1, 1, 1, 1 },
        { 1, 1, 1, 1 },
        { 1, 1, 1, 1 }
    };
    double arr2[][COLS] = {
        { 2, 2, 2, 2 },
        { 2, 2, 2, 2 },
        { 2, 2, 2, 2 },
        { 2, 2, 2, 2 }
    };
    if (1)
        temp = &arr1;
    else
        temp = &arr2;

    (*temp)[1][0] = 3;
    for(size_t r = 0; r < ROWS; r  ) {
        for(size_t c = 0; c < COLS; c  ) {
            printf("%lf%s", (*temp)[r][c], c   1 < COLS ? ", " : "\n");
        }
    }
}

and example run:

1.000000, 1.000000, 1.000000, 1.000000
3.000000, 1.000000, 1.000000, 1.000000
1.000000, 1.000000, 1.000000, 1.000000
1.000000, 1.000000, 1.000000, 1.000000

CodePudding user response:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define COL_COUNT 4

int main(int argc, const char * argv[]) {

    //double (*temp)[4][COL_COUNT];
    double (*temp)[COL_COUNT];// not need "row"
    double array1[4][COL_COUNT];
    double array2[4][COL_COUNT];
    
    int i,j;
    int nCount = 0;
    for (i=0; i<4; i  ) {//init some data
        for (j=0; j<COL_COUNT; j  ) {
            array1[i][j] = nCount;
            nCount  ;
        }
    }
    for (i=0; i<4; i  ) {//init some data
        for (j=0; j<COL_COUNT; j  ) {
            array2[i][j] = nCount;
            nCount  ;
        }
    }
    printf("array1 = \n");
    temp = array1;
    for (i=0; i<4; i  ) {//printf the array1 data
        for (j=0; j<COL_COUNT; j  ) {
            printf("%6.0lf ",temp[i][j]);
        }
        printf("\n");
    }
    printf("array2 = \n");
    temp = array2;
    for (i=0; i<4; i  ) {//printf the array2 data
        for (j=0; j<COL_COUNT; j  ) {
            printf("%6.0lf ",temp[i][j]);
        }
        printf("\n");
    }
    
    return 0;
}
  • Related