Pointer Declaration Array

I need some clarification on the array of pointers and more precisely how to declare them .
Take a look at this code:

main()
{
    int array[] = {5, 4, 2};
    int array2[] = {6, 8};
    int* int_arrays[2] = {array, array2}; // It works!

//  int* int_arrays2[2] =
//  {
//      {5, 4, 2},
//      {6, 8}
//  };
//
    int i, j;
    for(i = 0; i < 2; i++) 
    {
        for(j = 0; j < 3; j++) // This loop print a garbage value at the end, no big deal.
                printf("%d\n", int_arrays[i][j]);
    }
}

For me, a commented out ad means the same as above. But that does not work. The Visual Studio C compiler gives me these instructions:
Error: too many initializers.
warning: int * differs in indirection levels from int.

I suppose that means that int array[] = {5, 4, 2}- this is something that can be assigned int*
a {5, 4, 2}- no.
Could you show me a way to properly process my pointer?

+4
source share
3 answers

, . C99,

int* int_arrays2[2] =
{
     (int[]){5, 4, 2},
     (int[]){6, 8}
};

" ", , , , .

+5

int* int_arrays[2]

int_arrays int. , int " ". , , , , int , int *.

(, { 5, 4, 2}) , . , , . , :

char int_array_of_arrays[2][3] = { ... };

, : - , .


"", , , . C - , , undefined.

+3

int* int_arrays[2] = {array, array2}; 

, . , , .

int* int_arrays[2] = { &array[0], &array2[0] };

, int_arrays , , .

: .

, .

int x = { 10 };  // Okey
int y = ( 10, 20 }; // compiler error

int* int_arrays2[2] =
{
      {5, 4, 2},
      {6, 8}
};

int_arrays2 - . int *.

int *p1 = {5, 4, 2};
int *p2 = {6, 8};

p1 p2 int_arrays2[0] int_arrays2[1].

, , .

, .

,

int* int_arrays2[2] =
{
      ( int[] ){5, 4, 2},
      ( int [] ){6, 8}
};

, . , , , , .

.

+2

Source: https://habr.com/ru/post/1618576/


All Articles