pointer arithmetic

am I doing this right, or am I missing something here, lol.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>

int main()
{
    using namespace std;

    int array[3] = {1,2,3};
    int * parray;
    parray = &array[0];

    cout << "*parray: " << *parray << endl;

    cout << "adding 1 to *parray" << endl;

    parray += 1;

    cout << "parray: " << *parray << endl;

    cout << "adding 1 to *parray" << endl;

    parray += 1;

    cout << "parray: " << *parray << endl;

return 0;
}
Nope, it's all good.
Although you could just say pArray = array, since an
array is just a pointer to the first element of the "array".

You can use "[x]" on a pointer, and it returns
*(pArray+x).
For example: pArray[1] would return 2.
If you were then to increment pArray (++pArray)
pArray[1] would return 3.

So when you're using the "[x]" it's not magically
bound to arrays. Quite the opposite: an array is
simply a pointer to the first element in the "array".
you are not missing any thing just go ahead............
Topic archived. No new replies allowed.