1

What is the difference between:

//Example of "Complicated Array Declarations" from C++ Primer
int arr[10] = {1,2,3,4,5,6,7,8,9,10};
int (*Parr)[10] = &arr;

And:

int arr[10] = {1,2,3,4,5,6,7,8,9,10};
int *Parr = arr;

Both are pointers to an array of integers. But in order to access the first element of arr in the first snippet, I have to do **Parr whereas in the second, I only have to dereference once *Parr

1
  • 1
    No, the second one is not a pointer to an array. It's a pointer to the first element of an array. Commented Oct 3, 2014 at 6:14

3 Answers 3

8

In the first code sample,

int (*Parr)[10] = &arr;

Parr is a pointer to an array of 10 ints. It can only point to such an object. For example,

int (*Parr)[10];
int a[10];
Parr = &a; // OK
int b[42];
Parr = &b; // ERROR, b is of the wrong type

In the second code sample,

int *Parr = arr;

Parr is a pointer to int, initialized to point to the first element of arr. But it can point to any int.

int* Parr;
int a[10];
Parr = &a; // OK, a decays to int*
int b[42];
Parr = &b; // OK, b decays to int*
int c = 42;
Parr = &c; // OK, c is an int*
Sign up to request clarification or add additional context in comments.

4 Comments

Would you please explain why I need to dereference Parr twice?
@EngieOP It is a pointer to an array. So you need one de-reference to get to the array it points to, and another one to get to the element. You can also do (*Parr)[3].
Okay, that makes a lot more sense now. Thanks.
You could also consider int (*Parr)[10] to be a pointer to the first row of a [?][10] integer 2d matrix. Parr[0] would point to the first row, Parr[1] would point to the second row, and to index a particular integer in a matrix, you could use Parr[row][column].
2

First one is a 'pointer to int[10]'.

int (*Parr)[10] = &arr;  // point to int array

*Parr;   // *Parr == arr 
**Parr;  // **Parr == *(arr) == arr[0]

Second one is a 'pointer to int'

int *Parr = arr;         // point to the start of array
*Parr;   // *Parr == *(arr) == arr[0]

Comments

1

The way we can have a pointer to an integer,or a pointer to a foat ,can we also have a pointer to array? The answer is "YES".Declaration of a pointer to an array,however is a little clumsy.

For Example,The declaration int(*q)[4] means that q is a pointer to an array of 4 integer. in your Code

int(*parr)[10] means parr is a pointer to an array of 10 integer.

but int *parr=arr is only a pointer to the oth element of arr[10].

So suppose you assgin any pointer to arr[10]. in the second case doing parr++.the parr will move to location arr[10] form arr[0]. but in the second case doing parr++.the parr will move to location arr[1] form arr[0].

So i hope u got the answer.

Comments

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.