22
 int (*a)[5];

How can we Initialize a pointer to an array of 5 integers shown above.

Is the below expression correct ?

int (*a)[3]={11,2,3,5,6}; 
4
  • There's never any good use for "pointer to array" in C. It just obscures the code for no benefit. Just declare and initialize the array, and use pointers to its element type. Commented Jul 25, 2013 at 7:06
  • 9
    @LeeDanielCrocker: That's not true - a pointer to array is the most efficient way to dynamically allocate a multidimensional array (as in int (*a)[5] = malloc(nrows * sizeof *a);) Commented Jul 25, 2013 at 7:24
  • Yeah, that is pretty simple, but that only applies when you know the inner dimension at compile time, not in the more general case. Commented Jul 25, 2013 at 8:08
  • 3
    @LeeDanielCrocker: works just as well for VLAs: size_t cols; ...; int (*a)[cols] = malloc( nrows * sizeof *a );. It's also what a 2D array expression decays to when passed to a function. The OP's usage is incorrect, but that doesn't make them useless. Commented Jul 25, 2013 at 9:57

4 Answers 4

23

Suppose you have an array of int of length 5 e.g.

int x[5];

Then you can do a = &x;

 int x[5] = {1};
 int (*a)[5] = &x;

To access elements of array you: (*a)[i] (== (*(&x))[i]== (*&x)[i] == x[i]) parenthesis needed because precedence of [] operator is higher then *. (one common mistake can be doing *a[i] to access elements of array).

Understand what you asked in question is an compilation time error:

int (*a)[3] = {11, 2, 3, 5, 6}; 

It is not correct and a type mismatch too, because {11,2,3,5,6} can be assigned to int a[5]; and you are assigning to int (*a)[3].

Additionally,

You can do something like for one dimensional:

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

Similarly, for two dimensional try this(thanks @caf):

int (*a)[5] = (int p[][5]){ { 1, 2, 3, 4, 5 } , { 6, 7, 8, 9, 10 } };
Sign up to request clarification or add additional context in comments.

12 Comments

@Flow second not correct try ,as int (*a)[5]= (int(*)[5])&({11,2,3,5,6}); with new compiler
int (*a)[5] = (int [][5]){ { 1, 2, 3, 4, 5 } , { 6, 7, 8, 9, 10 } };
@caf got it Thanks its similar to: int *why = (int[2]) {1,2}
@caf got it!! my commented expression is not correct because I am using & before constant
@caf your expression is c99 ?
|
5

{11,2,3,5,6} is an initializer list, it is not an array, so you can't point at it. An array pointer needs to point at an array, that has a valid memory location. If the array is a named variable or just a chunk of allocated memory doesn't matter.

It all boils down to the type of array you need. There are various ways to declare arrays in C, depending on purpose:

// plain array, fixed size, can be allocated in any scope
int array[5] = {11,2,3,5,6};
int (*a)[5] = &array;

// compound literal, fixed size, can be allocated in any scope
int (*b)[5] = &(int[5]){11,2,3,5,6};

// dynamically allocated array, variable size possible
int (*c)[n] = malloc( sizeof(int[n]) );

// variable-length array, variable size
int n = 5;
int vla[n];
memcpy( vla, something, sizeof(int[n]) ); // always initialized in run-time
int (*d)[n] = &vla;

1 Comment

@M.M Indeed. Not sure why I wrote that. Fixed. I suppose I was trying to warn about passing a pointer to a compound literal outside the scope where it was declared.
1
int a1[5] = {1, 2, 3, 4, 5};
int (*a)[5] = &a1;

Comments

0
int vals[] = {1, 2};
int (*arr)[sizeof(vals)/sizeof(vals[0])] = &vals;

and then you access the content of the array as in:

(*arr)[0] = ...

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.