1

enter image description here

int *p = ( int[] ){ 1, 2, 3, 4 };

Doing this I am able to initialize a anonymous array to a pointer p. In a similar way I want to assign arrays of array (i.e. probably 2D array) in array of pointers. And so I have tried the following code:

int *p[]= (int [][3]) { {1,2,3},{10,20,30} };

But it goes wrong, anyway if I assign it to a pointer to whole Array( as int (*p)[]= (int [][3]) { {1,2,3},{10,20,30} }; ) it works fine. I am in confused that if a pointer can get assigned a anonymous array why the array of pointers could not get assigned to 2d array?

2
  • 2
    Please elaborate "it goes wrong" and provide a minimal reproducible example each for both described cases. Also pleas explain what you intend to do this way. I suspect that there is a better way. Compare meta.stackexchange.com/questions/66377/what-is-the-xy-problem Commented Jul 4, 2022 at 5:52
  • If X is a type, then an array of X decays to a pointer of X. Now say X is array-of-Y. An array of X still decays to a pointer to X, that is, a pointer to an array-of-Y. It does not decay to an array of pointers to anything, nor to a pointer to a pointer to anything. Commented Jul 4, 2022 at 6:10

2 Answers 2

1

The compound literal

(int [][3]) { {1,2,3},{10,20,30} };

has the type

int [2][3]

i.e. array of 2 elements, each consisting of 3 int elements.

When used in the line

int *p[]= (int [][3]) { {1,2,3},{10,20,30} };

the array will decay to a pointer to the first element. This pointer will therefore have the type "pointer to array of 3 int elements`.

However, you cannot assign this data type to p, because the types are different. You declared p as an array of pointers. That is why your code is not working.

If you want p to be an array of pointers in which every pointer points to its array of int elements, then you will need to use several compound literals, one for each of these arrays:

int *p[] = {
    ( int[] ){  1,  2,  3,  4 },
    ( int[] ){  5,  6,  7,  8 },
    ( int[] ){  9, 10, 11, 12 }
};

Here is a small test program:

#include <stdio.h>

int main( void )
{
    int *p[] = {
        ( int[] ){  1,  2,  3,  4 },
        ( int[] ){  5,  6,  7,  8 },
        ( int[] ){  9, 10, 11, 12 }
    };

    printf( "%d\n", p[1][2] );
}

This program has the output 7.

EDIT:

According to your remarks in the comments section of this answer, it seems that you have several 2D arrays and want to create an array of pointers to these arrays. In that case, you can do the following:

#include <stdio.h>

int main( void )
{
    //define first 2D array
    int arr1[][4] = {
        {  1,  2,  3,  4},
        {  5,  6,  7,  8},
        {  9, 10, 11, 12}
    };

    //define second 2D array
    int arr2[][4] = {
        { 13,  14, 15, 16},
        { 17,  18, 19, 20},
        { 21,  22, 23, 24}
    };

    //define array of 2 pointers that point to the first
    //rows of arr1 and arr2
    int (*p[])[4] = { arr1, arr2 };

    //use pointer array to get the 4th element in the
    //3rd row of the second 2D array
    printf( "%d\n", p[1][2][3] );
}

This program will give you the correct output 24.

If you find the declaration int (*p[])[4] hard to understand, then you may want to read this page on how these declarations are to be interpreted. Also, this site may be useful.

You can also simplify your declarations by using a typedef, so that the type arrow_row represents an array of 4 int elements (i.e. a row of a 2D array) like this:

typedef int array_row[4];

Now, you can simplify the declaration

int (*p[])[4] = { arr1, arr2 };

to:

array_row *p[] = { arr1, arr2 };
Sign up to request clarification or add additional context in comments.

11 Comments

Doing this, Your are assigning different 1D arrays to an array of pointers. Thanks for this. Also suggest me how could I assign 2D array in an array of pointers.
@AnupAdhikari: How exactly do you want a 2D array assigned to an array of pointers? Do you want every element of the array of pointers to point to the start of a row in the 2D array?
yeah , all I was saying exactly the same you are taking about so that I could access 2D array
@AnupAdhikari: You can construct such an array of pointers in a loop. See this working code example. You could also hard-code the pointer array, like this: int *p[] = { arr[0], arr[1], arr[2] };, but then you would have to change this definition whenever the number of rows changes. If you use the code I posted in the link, then it will adapt to the number of lines automatically, and you will never have to change the definition of p.
@AnupAdhikari: Note that what you are asking for is rather strange. I therefore suspect that you are asking about an XY problem. You may want to explain what your ultimate goal is.
|
1

int *p[] is an array of pointers, not a pointer to an array.

You'll need to do:

int (*p)[3]= (int [][3]) { {1,2,3},{10,20,30} };

The reason why is that an array, whenever used in an expression, decays into a pointer to the first element. So no matter the array type, your pointer will need to correspond to the pointer to the first element.

In case of an int[], the element type is int and a pointer to it would be int*.

In case of an int[][3], the element type is int[3] and a pointer to it would be int (*p)[3].

6 Comments

Please have a look at my image too. All I am saying is that if I can assign a array or address of an array(whatever it treats internally) to a pointer (p) why can't I assign a collection of those array which is in fact 2D array in a array of similar pointers.
@AnupAdhikari Because in that case you need to do int* p [] = { arr[0], arr[1], ...} which would be a rather cumbersome and generally pointless array to have.
Good to say that int (*p)[] = (int [][3]) { {1,2,3},{10,20,30} }; is also valid, is a pointer to an array with unspecified bounds, as such you can not use sizeof nor dereference this kind of pointers without a cast, in your case ((int (*)[3])p)[1][1] will give you 20.
@Lundin could you please elaborate and show me coding
@DavidRanieri could you please elaborate and show me coding?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.