3

I have learnt that the name of the array is actually the address of array_name[0]. Then why does is require to add ampersand sign before the name of the array while initializing a pointer to the array.

  int (*pointer_name)[5] = &array_name;

I have tried:

  int *pointer_name = array_name; 

and it works fine. What is the difference between the two other than the "type of pointer"? And also what are the pros-cons of either of them. When to use them? Does anyone of them has got any greater/ better functionality over other?

1
  • "I have learnt that the name of the array is actually the address of array_name[0]". You learnt incorrectly. Array name can act as (pretend to be) a pointer to array_name[0] in certain contexts, but not in all contexts. And you definitely cannot say that the name of the array is the address of array_name[0]. Commented Nov 15, 2016 at 18:41

3 Answers 3

3
int *pointer_name = array_name;

declares a pointer to int that points to the first int of the array array_name.

int (*pointer_name)[5] = &array_name;

declares a pointer to an array of 5 ints that points to the array array_name.

Addresses are the same but types not.

If you use pointer arithmetic on those you have, in the first case:

pointer_name++;

will just point to the second int of the array, while in the second case it will point just after the whole array.

Sign up to request clarification or add additional context in comments.

Comments

2

Then why does is require to add ampersand sign [..]. I have tried : int *pointer_name = array_name; And it works fine.

Because the types are different.

  • &array_name is a pointer to an array of 5 ints and has type: int (*)[5].

  • array_name gets converted into a pointer to its first element when you assign it to pointer_name (which is equivalent to &array_name[0]) and has type: int*.

If array_name is an array of 5 ints then both:

int (*pointer_name)[5] = &array_name;

and

int *pointer_name = array_name;

are valid. Just how you'd access them later through these two pointers is different.

2 Comments

I have checked. Both kind of pointer has size = 8 byte. And the value stored by them is also same. What is the difference between the two. Are they same in respect to functionality?
The address will be same (&array_name == &array_name[0] == array_name). But how you can access that array differs. With int *pointer_name = array_name;, you can access it like: pointer_name[0] /*array_name[0] */ or *pointer_name /*array_name[0] */; *(pointer_name + 1) /* array_name[1] */. With int (*pointer_name)[5] = &array_name;, you can access it like: (*pointer_name)[1]; /* array_name[1] */. Both ways are fine. But the main difference is how pointer arithmetic works on these types.
0

The type of the object pointed to by the pointer declared like

int (*pointer_name)[5] = &array_name;

is int[5] . This means for example that this operator

sizeof( *pointer_name ) 

yields the value equal to 5 * sizeof( int ). And that if to use the pointer arithmetic as for example pointer_name + 1 then the address obtained by this expression will be equal to the address stored in the pointer plus value of 5 * sizeof( int ).

The type of the object pointed to by the pointer declared like

int *pointer_name = array_name;

is int . This means for example that this operator

sizeof( *pointer_name ) 

yields the value equal to sizeof( int ). And that if to use the pointer arithmetic as for example pointer_name + 1 then the address obtained by this expression will be equal to the address stored in the pointer plus value of sizeof( int ).

A pointer declared like this

int (*pointer_name)[5];

usually used for two-dimensional arrays that points to "rows" of array.

For example

int array_name[2][5];

int (*pointer_name)[5] = array_name;

// outputs 20 provided that sizeof( int ) is equal to 4
printf( "%zu\n", sizeof( *pointer_name ) ); 

// outputs 4 provided that sizeof( int ) is equal to 4
printf( "%zu\n", sizeof( **pointer_name ) ); 

pointer_name points to the first row of the array array_name. pointer_name + 1 points to the second row of the array.

2 Comments

In the second line of the code snippet just above it who have not used the ampersand sign before "array_name". Why is it that address operator is not required here. And also someone mentioned it in the comments that "And you definitely can not say that the name of the array is the address of array_name[0]," why is it so?
@ShawAnkush I have not understood what you are asking. If you mean this code snippet int array_name[2][5]; int (*pointer_name)[5] = array_name; then there is not required ampersand.

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.