[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.
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.
Re: A pointer to an array: int* p = arr; vs. int (*p2)[10] = &arr; ?
Quote:
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.
Re: A pointer to an array: int* p = arr; vs. int (*p2)[10] = &arr; ?
Quote:
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]))
Re: A pointer to an array: int* p = arr; vs. int (*p2)[10] = &arr; ?
I see, thanks guys for all responses.