Home > Mobile >  Relationship matrix-pointer
Relationship matrix-pointer

Time:10-05

I was wondering why in C this is possible:

    int MATRICE[20][20];
int *p; p = MATRICE[19]; is equal to p = &(MATRICE[19][0]);

I tried to interpretate it in this way: I consider the label "MATRICE" as a constant pointer and like an array of pointers, so when it comes to the 20th pointer (MATRICE[19]) it points at the same thing that MATRICE[19][0] points too. Is my idea correct?

CodePudding user response:

Arrays used in expressions with rare exceptions are converted to pointers to their first elements.

From the C Standard (6.3.2.1 Lvalues, arrays, and function designators)

3 Except when it is the operand of the sizeof operator or the unary & operator, or is a string literal used to initialize an array, an expression that has type ‘‘array of type’’ is converted to an expression with type ‘‘pointer to type’’ that points to the initial element of the array object and is not an lvalue. If the array object has register storage class, the behavior is undefined.

This expression

MATRICE[19]

yields a one-dimensional array of the type int[20] that is implicitly converted to pointer to its first element of the type int * when used as an initializer (as a right side hand expression in the assignment) in this code snippet

int *p; p = MATRICE[19];
  • Related