0

I really do not understand why i keep on getting this error "Array type 'double [10][10] is not assignable' " I tried passing an array inside and it still doesn't seem to work

Aren't I allow do this. What is an alternative ? Thanks for your help

   #define MAX 10
//structures usually defined at top along with function prototypes
typedef struct {
    unsigned int row;
    unsigned int col;
    double array [MAX][MAX];
}Matrice;

Matrice lire_matrice(void);
Matrice multiplication( Matrice a,  Matrice b);
void affiche_matrice(Matrice m);

int main(int argc, const char * argv[]) {
    //insert code here...


    Matrice m1 = lire_matrice();
    Matrice m2 = lire_matrice();
}

Matrice lire_matrice(void){
    unsigned int row,col;

    printf("Enter row which must be smaller or equal to %d ", MAX);
    scanf("%d",&row);

    printf ("Enter col which must be smaller or equal to %d",MAX);
    scanf("%d",&col);

    double table[row][col];
    int i;
    int j;
    double input;
    for(i = 0; i < row; i++){
        for(j= 0; i < col;j++){
            printf("M[%d,%d] =",i,j);
            scanf("%lf",&input);
            table[i][j]= input;
        }
    }

    Matrice m;



    m.array = table; <<**ERROR ARRAY TYPE DOUBLE[10][10] IS NOT ASSIGNABLE**>>   
    m.row = row;
    m.col = col;


    return m;

}
Nameless One
  • 1,615
  • 2
  • 23
  • 39
franck
  • 63
  • 2
  • 5
  • Array are not copyable by default. You should implement a method or function that copy data from one struct to another. – Jepessen Feb 07 '15 at 12:08
  • Welcome to stackoverflow, your question seems to be a duplicate of https://stackoverflow.com/questions/3755459/question-on-equating-arrays-in-c – Nameless One Feb 07 '15 at 12:13

2 Answers2

1

From the C-FAQ:

For a two-dimensional array like

int array[NROWS][NCOLUMNS];

a reference to array has type pointer to array of NCOLUMNS

Change to:

typedef struct {
    unsigned int row;
    unsigned int col;
    double (*array)[MAX];
}Matrice;

Or leave it as is and

memcpy(m.array, table, sizeof table);
David Ranieri
  • 39,972
  • 7
  • 52
  • 94
1

Arrays in C are constant pointers to the first element. In line m.array = table; you want to change this constant pointer to point the m.array to the memory location table. This is not allowed in C.

You have to possible solutions.

1.) In struct Matrice you define array element as a pointer to an array. A possible (but not the unique) solution is the following:

typedef struct {
    unsigned int row;
    unsigned int col;
    double *array;
} Matrice;

It that case you can refer on the element (i, j) - i is the row index, y is the column index - of the matrix of the following way:

Matrice m;
double table[row][col];
m.array = &table[0][0];
double elem = m.array[j + i * col];

I never worked in environments where this solution does not work but according to Alter Mann's notes this solution can cause undefined behaviour. Namely m.array will point to the 0th element of the table[0] which is an array with col elements. It is possible that the compiler "uses" this information (i.e. m.array points to an array which has col elements), but the program uses m.array as an array with row*xcol elements. This can cause undefined behaviour. But I left here this solution because "pratically" it works well.

Better solution is if also table is a 1 dimensional array:

Matrice m;
double table[row * col];
m.array = &table[0];
double elem = m.array[j + i * col];

2.) The other possible solution is that you implement a method which copies the element from a double[][] "table" to the matrix array.

Tibor Takács
  • 3,535
  • 1
  • 20
  • 23
  • `double *array = &table[0][0];` in practice it works but behavior is not defined, [take a look](http://stackoverflow.com/questions/25303647/accesing-a-2d-array-using-a-single-pointer) – David Ranieri Feb 07 '15 at 12:53
  • The question you offered is about the dereferenicing rule in the end of an array. But in several questions it is written that 2d arrays are stored in contiguous memory area and you can address it like in my code (http://stackoverflow.com/questions/7784758/c-c-multidimensional-array-internals/7785116#7785116 or http://stackoverflow.com/questions/1242705/performance-of-2-dimensional-array-vs-1-dimensional-array). Therefore I think my solution works correct but I am interesting your opition. Can you explain me what you mean? – Tibor Takács Feb 07 '15 at 15:29
  • I asked the question and is not about the dereferencing rule in the end of an array, is about accessing a 2D array using a single pointer, dereferencing from `table[1][0]` to `table[MAX][MAX]` using a single pointer to the begin of a 2D array is UB, you are only allowed to dereference from `table[0][0]` to `table[0][MAX]`,read more about this point [in this thread](http://stackoverflow.com/a/7787436/1606345) – David Ranieri Feb 07 '15 at 17:28
  • 1
    @AlterMann thank you for your notices. I read all the threads in Stackoverflow about this topic, now I understand the problem: `m.array` will points to the 0th element of the `table[0]` which is an array with `col` elements. It is possible that the compiler "uses" this information (i.e. `m.array` pointa an array which has `col` elements), but in my code I uses `m.array` as an array with `row`*`col` elements, and this can cause undefined behaviour. I left this solution in my answer because it work pratically, but I suggested a new solution as well. Thank you for you notice! – Tibor Takács Feb 08 '15 at 14:00