Pointers and array problem.

So I'm experimenting with pointers and arrays trying to get the feel of using and understanding them more. The *ptArray value in my source seems to change and not stay equal to 0 After the 2nd for loop? I'm puzzled as to why this occurs I thought *ptArray would retain its value of 0.

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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <iostream>

using namespace std;

int main()
{
    int myarray[5] = {0,1,2,3,4};
    int* ptArray = myarray;

    cout << "1. *ptArray now " << *ptArray << endl;
    cout << "2. myrray now " << myarray[0] << endl;

    if(*ptArray == myarray[0])
    {
        cout << "*ptArray == 0" << endl;
    }


    for(unsigned int i = 0; i < sizeof(myarray)/sizeof(int); i++)
    {
        cout << ptArray[i] << " " << flush;
    }

    cout << endl;

    for(unsigned int i = 0; i < sizeof(myarray)/sizeof(int); i++)
    {
        cout << *ptArray << " " << flush;

        ptArray++;

    }

    cout << endl;

    cout << "3. *ptArray now " << *ptArray << endl;
    cout << "4. myarray now " << myarray[0] << endl;
    if(*ptArray == myarray[0])
    {
        cout << "*ptArray == 0" << endl;
    }
    else
    {
        cout << "*ptArray != 0" << endl;
    }


    int *firstElement = &myarray[0];
    int *lastElement =  &myarray[4];



    while(true)
    {

        cout << *firstElement << " " << flush;
        if(*firstElement == *lastElement)
        {
            break;
        }
        firstElement++;

    }



    return 0;
}


Screenshot of me running the console app: https://gyazo.com/857bd22480ed7d76db07c737108813d0

I believed Line 1. and .3 should've been equivalent ??

Last edited on
Try using the address-of operator (&), but you cannot point to the whole array. I would use a for loop and take the address of one array value at a time like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//declare variables
int array[5] = {0, 5, 6, 3, 4};
int *pointer = nullptr; //set to NULL or nullptr
    
//loop through array
for (int i = 0; i < 5; i++)
{
    //store address of array value in pointer
    pointer = &array[i];
        
    //display values
    cout << "array[" << i << "] value: " << array[i] << endl;
    cout << "pointer value: " << pointer << endl;
    cout << "*pointer value: " << *pointer << endl << endl;
} //end for 

Output:
array[0] value: 0
pointer value: 0x7fff5fbff760
*pointer value: 0

array[1] value: 5
pointer value: 0x7fff5fbff764
*pointer value: 5

array[2] value: 6
pointer value: 0x7fff5fbff768
*pointer value: 6

array[3] value: 3
pointer value: 0x7fff5fbff76c
*pointer value: 3

array[4] value: 4
pointer value: 0x7fff5fbff770
*pointer value: 4

Program ended with exit code: 0

If you want to make a pointer array, you would put what I have below. The word new allocates memory for the array:

int *ptrArray = new int [2] //start off with two elements
Last edited on
A visualization of what happens when you increment a pointer:
myarray:
1 2 3 4 5
↑
ptarray (*ptarray = 1)

++ptarray

myarray:
1 2 3 4 5
  ↑
ptarray (*ptarray = 2)
As you increment it in loop size of array times, it now points to the garbage outside of array.
Topic archived. No new replies allowed.