About Pointers and arrays in C++ -


i ask question pointers , arrays in c++.

int a[10];  int *p1; p1 = &a[0];  int *p2; p2 = a;  int (*p3)[10]; p3 = &a; 

what differences between p1, p2 , p3? confusing.

firstly, a array of 10 ints. that's easy part.

p1 "pointer int". assigning value of &a[0]. takes address of first element of a. p1 points first element of a.

p2 "pointer int". assigning a directly it. in case, standard conversion must take place called array-to-pointer conversion. basically, array can converted pointer first element. assigning result of conversion p2. p2 also pointer first element of a.

p3 "pointer array of 10 int". taking address of array a , assigning pointer. pointer points @ array (not first element of it).

you might think "well first element has same address array, what's difference?" in fact, you'll notice difference when try increment pointer. incrementing either p1 or p2 give pointer second element of array. incrementing p3 give pointer next array of 10 ints (which doesn't exist).

┌─────┬─────┬─────┬─────┬─────┬─────┬─────┬─────┬─────┬─────┐ │ int │ int │ int │ int │ int │ int │ int │ int │ int │ int │  └─────┴─────┴─────┴─────┴─────┴─────┴─────┴─────┴─────┴─────┘    ^    └── p1, p2, p3 

so if start off pointing have described , increment them, get:

┌─────┬─────┬─────┬─────┬─────┬─────┬─────┬─────┬─────┬─────┬┄ │ int │ int │ int │ int │ int │ int │ int │ int │ int │ int │  └─────┴─────┴─────┴─────┴─────┴─────┴─────┴─────┴─────┴─────┴┄          ^                                                     ^          └── p1, p2                                            └── p3 

Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -