-
March 31st, 2013, 10:07 PM
#1
[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.
-
March 31st, 2013, 10:29 PM
#2
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.
-
April 2nd, 2013, 05:11 AM
#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.
Cheers, D Drmmr
Please put [code][/code] tags around your code to preserve indentation and make it more readable.
As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky
-
April 2nd, 2013, 08:24 AM
#4
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]))
-
April 3rd, 2013, 09:09 AM
#5
Re: A pointer to an array: int* p = arr; vs. int (*p2)[10] = &arr; ?
I see, thanks guys for all responses.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|