Which definition is better?

#include<stdio.h>
int main(int argc , char *argv[])
{
    int array[2][2] = {{1,100},{1000,10000}};

    int *pointer    = array;
    int *ppointer   = &array;
    int *pppointer  = array[0];
    int *ppppointer = &array[0];

    printf("%d\n",*pointer);
    printf("%d\n",*ppointer);
    printf("%d\n",*pppointer);
    printf("%d\n",*ppppointer);

    return 0;
}

Four pointers point to the first element of the array. which definition shown above is better? And I don't know why the same value for array and & array?

+5
source share
7 answers

The only reason to compile your definitions is because your C compiler is too permissive when it comes to pointer type conversions. If you use some switches that make it more pedantic in this regard, he should immediately tell you that only the third initialization is valid, and the rest are erroneous.

( ), T[N], "" ( ) T * - , . , A A &A[0]. , , - &, sizeof , char.

array - int [2][2]. int (*)[2].

int *pointer    = array;

int (*)[2], - int *. . .

int *ppppointer = &array[0];

: int (*)[2]. .

&array int (*)[2][2]. ,

int *ppointer   = &array;

.

yo -

int *pppointer  = array[0];

array[0] - int [2], int * type - , .

, , "". , .

int *pppointer  = &array[0][0];

, . , "" (array[0] &array[0][0]) .


,

int (*pointer)[2]     = array;
int (*ppointer)[2][2] = &array;
int (*ppppointer)[2]  = &array[0];

int *. , -, int * .

+8

. ++ C. C, ++, C++ C (MSVC). C- ++ .

1, 2 4? int*.

  • array int[2][2], int(*)[2], int*
  • &array int[2][2]
  • array[x] int*
  • &array[x] int**
+2
int *pointer    = array;     //Incorrect
int *ppointer   = &array;    //Incorrect
int *pppointer  = array[0];  //Correct
int *ppppointer = &array[0]; //Incorrect

. .

, "" ( - )... int, int * []

, ... , , .

, int, .

, . int **, int *.

... , , .

+1

:

$cat decls.c

int main(void)
{
    int array[2][2] = {{1,100},{1000,10000}};
    int *pointer    = array;
    int *ppointer   = &array;
    int *pppointer  = array[0];
    int *ppppointer = &array[0];
}

$clang decls.c -Wall -o decls

decls.c:4:7: warning: incompatible pointer types initializing 'int *' with an
      expression of type 'int [2][2]' [-Wincompatible-pointer-types]
        int *pointer    = array;
             ^            ~~~~~
decls.c:5:7: warning: incompatible pointer types initializing 'int *' with an
      expression of type 'int (*)[2][2]' [-Wincompatible-pointer-types]
        int *ppointer   = &array;
             ^            ~~~~~~
decls.c:7:7: warning: incompatible pointer types initializing 'int *' with an
      expression of type 'int (*)[2]' [-Wincompatible-pointer-types]
        int *ppppointer = &array[0];
             ^            ~~~~~~~~~

, .

: - C, , .

, char name[][], , name[2][3], char. : let A = name[3]; char A? A[2], A char *.

: .

0

( , , , ).

:

int ** pointer1    = array;
int ** pointer2   = &array;        //This one is wrong
int ** pointer3  = array[0];      //This one is not correct in this case
int * ppointer3  = array[0];
int ** pointer4 = &array[0];
int * pointer5 = &array[0][0];

.

1- , , , . , ( , : , 1000, pointer5[2] pointer5[1][1]

0

.

& array - , , , array = & array [0]

array - , , = & array = & array [0]

  int *pppointer = array[0];

, . - .

. , , :

int *pppointer = (&array)[0] = array[0]

-1

, . , "array" .

, :

int *pointer = array;

. *pointer int, array .

:

int *ppointer = &array;

. & array , .

:

int *pppointer = array[0];

. . , array[0] int, *pppointer.

:

int *ppppointer = &array[0];

, , . array[0] int, &array[0] - int, int.

So, in the end, the third is the only one that is really valid. However, I personally believe that the best way to achieve this would be:

int *pointer = *array;
-2
source

All Articles