[RESOLVED] A pointer to an array: int* p = arr; vs. int (*p2)[10] = &arr; ?
 CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com

# Thread: [RESOLVED] A pointer to an array: int* p = arr; vs. int (*p2)[10] = &arr; ?

1. Member
Join Date
Jan 2012
Location
USA
Posts
91

## [RESOLVED] A pointer to an array: int* p = arr; vs. int (*p2)[10] = &arr; ?

Hello,

Code:
```int arr[10];
int* p = arr;
int (*p2)[10] = &arr;```
So, pointer p is a pointer to an array, I can use it to access elements of arr as in *(p+5).

Pointer p2 is a pointer to an array of ten integers. What is it for, how can I use it to access elements of arr?

Thank you.

2. Elite Member Power Poster
Join Date
Jan 2006
Location
Singapore
Posts
6,734

## Re: A pointer to an array: int* p = arr; vs. int (*p2)[10] = &arr; ?

In this case, there probably is not much use for p2. However, if you were dealing with a 2D array, then it could become useful.

3. ## Re: A pointer to an array: int* p = arr; vs. int (*p2)[10] = &arr; ?

Originally Posted by vincegata
So, pointer p is a pointer to an array, I can use it to access elements of arr as in *(p+5).
Strictly, p is a pointer to the first element of an array.

4. Elite Member Power Poster
Join Date
Apr 2000
Location
Belgium (Europe)
Posts
4,626

## Re: A pointer to an array: int* p = arr; vs. int (*p2)[10] = &arr; ?

Strictly, p is a pointer to the first element of an array.
true, but in the way the 2nd pointer is defined in the OP, it's a pointer to an array of (an array of) 10 ints (and not a pointer to (an array of) ints). and p2[0] points to the first of said arrays of 10 ints.
p2[1] would point to another array of 10 ints.

you can access the first int - element as p2[0][0] (=return the 1st int of the 1st array of 10 ints).
or alternatively you could (for the first element) also write this as: (*p2)[0] (the parens are significant here. or you'll get *(p2[0]))

5. Member
Join Date
Jan 2012
Location
USA
Posts
91

## Re: A pointer to an array: int* p = arr; vs. int (*p2)[10] = &arr; ?

I see, thanks guys for all responses.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•