Reputation: 4364
I have a pointer to an array (2d array). I like to assign 2d array to a pointer
int (*p)[2]={{1,2}};//SegFault
int (*p)[2]=(int **){{1,2},{3,4}}; //should it be same as above? but also causing segFault/
int (*p)[2]=(int[][]){{5,4},{5,6}};//should it be same as above two? but also causing segFault
And how can I access the values. should it be correct *(*(p)
to access of first array and first value. // Willl it also work p[0][0]
I think so, but please clarify that are these access methods are purely how C is designed or Is there any memory matters if I use *(*(p))
or p[0][0]
Upvotes: 2
Views: 49
Reputation: 311038
This declaration
int (*p)[2]={{1,2}};
is syntactically incorrect. Pointers are scalar objects and they may be initialized by a single expression optionally enclosed in braces.
In this declaration
int (*p)[2]=(int **){{1,2},{3,4}}
you are trying to use a compound literal but again the initialization is incorrect because an object of the type int **
is a scalar object and my be initialized by a single expression.
In this declaration with a compound literal
int (*p)[2]=(int[][]){{5,4},{5,6}};
there is used an incorrect type specifier int[][]
for a two-dimensional array because the size of elements of the array is unknown. You could write for example
int (*p)[2]=(int[][2]){{5,4},{5,6}};
That is you need to have an array that you will assign to a pointer. If you do not have such an array you can use a compound literal. For example
int (*p)[2] = (int[2][2]){{1,2},{3,4}};
or
int (*p)[2] = (int[][2]){{1,2},{3,4}};
Here is a demonstrative program.
#include <stdio.h>
int main(void)
{
enum { N = 2 };
int ( *p )[N] = ( int[N][N] ){ { 1, 2 }, { 3, 4 } };
for ( size_t i = 0; i < N; i++ )
{
for ( size_t j = 0; j < N; j++ )
{
printf( "%d ", p[i][j] );
}
putchar( '\n' );
}
putchar( '\n' );
int a[N][N] = { { 1, 2 }, { 3, 4 } };
p = a;
for ( size_t i = 0; i < N; i++ )
{
for ( size_t j = 0; j < N; j++ )
{
printf( "%d ", p[i][j] );
}
putchar( '\n' );
}
putchar( '\n' );
int b[N] = { 1, 2 };
p = &b;
for ( size_t i = 0; i < N; i++ )
{
printf( "%d ", ( *p )[i] );
}
putchar( '\n' );
return 0;
}
The program output is
1 2
3 4
1 2
3 4
1 2
Upvotes: 2
Reputation: 213989
{{1,2}};
is an array initializer, not an array. So the code is invalid C - you can't initialize a pointer like that. It's the wrong type int
and the wrong number of initializers.(int **){{1,2},{3,4}};
is a compound literal of type int**
. But again, the initialization of a pointer like this is invalid C.(int[][]){{5,4},{5,6}};
is an array of incomplete type. It can't be initialized. So this too is invalid C.All of your problems originate from ignoring compiler warnings. In case of gcc-like compilers I strongly recommend to change your settings to -std=c11 -pedantic-errors
. Then you'll get compiler errors when you write invalid C, instead of just warnings.
To answer the question, the correct way is:
int (*p)[2] = (int[2][2]){ {1,2}, {3,4} };
This creates a local scope 2D array with the same scope as the pointer p
. This is equivalent to:
int arr[2][2] = { {1,2}, {3,4} };
int (*p)[2] = arr;
Upvotes: 2