Address of pointer in myarray changes

In 4.4 Pointers and arrays part:
I want to display the updated mypointer content step-by-step. So, I used the following piece of code:

In the first step: by changing *mypointer = 10, I see the address of the pointer is changed and the output is not as desired. I must introduce mypointer = myarray again before *mypointer = 10 to have a correct output. Why?

Also, for the next step: changing *mypointer = 20 by this lines:
mypointer++;
*mypointer = 20;
will have a wrong output, and I have to change it to have a correct output.

These are my outputs:
mypointer after edit1:
10 , -1304110628 , 4197696 , 0 , 1697339456 , 32582 ,
mypointer after edit1-1:
10 , 8 , 15 , 16 , 23 , 42 ,
mypointer after edit2:
20 , 15 , 16 , 23 , 42 , 10 ,
mypointer after edit2-2:
10 , 20 , 15 , 16 , 23 , 42 ,

Could you explain to me why???

Hi I just executed that code and it seems to do what it’s supposed to do which is modify the first element of the array.

Try it here for fast testing:

// Online C++ compiler to run C++ program online
#include <iostream>

int main() {
    int myarray[6] = {4,8,15,16,23,42};
    int *mypointer;
    mypointer = myarray ;
    std::cout << std::endl << "My pointer after edit" << std::endl;
    *mypointer = 10;
    for (int i = 0; i < 6; i++){
        std::cout << *mypointer << " , ";
        mypointer++;
    }
    
    
    mypointer = myarray ;
    std::cout << std::endl << "My pointer after edit 2" << std::endl;
    *mypointer = 10;
    for (int i = 0; i < 6; i++){
        std::cout << *mypointer << " , ";
        mypointer++;
    }
    
    
    return 0;
}

I don’t know what else you were doing before, it looks like an issue while modifying pointer contents directly, but I’m not sure. We would need a Rosject or the code to test further.

Hi @maryseraj.1990,

You said:

I must introduce mypointer = myarray again before *mypointer = 10 to have a correct output. Why?

I’ve modified your code (the code posted by @duckfrost2) to print the address where mypointer is pointing to:

#include <iostream>

int main() {
    int myarray[6] = {4,8,15,16,23,42};
    int *mypointer;
    mypointer = myarray ;
    std::cout << std::endl << "My pointer after edit 1:" << std::endl;
    *mypointer = 10;
    for (int i = 0; i < 6; i++){
        // -----------------------------------
        // 🔥️ I modified the line below
        // -----------------------------------
        std::cout << *mypointer << " at " << mypointer << std::endl;
        mypointer++;
    }

    mypointer = myarray ;
    std::cout << std::endl << "My pointer after edit 1-1: " << std::endl;
    *mypointer = 10;
    for (int i = 0; i < 6; i++){
        std::cout << *mypointer << " , ";
        mypointer++;
    }

    return 0;
}

When I run this code, this is the output I have:

My pointer after edit 1:
10 at 0x7ffe4c6f3d70
8 at 0x7ffe4c6f3d74
15 at 0x7ffe4c6f3d78
16 at 0x7ffe4c6f3d7c
23 at 0x7ffe4c6f3d80
42 at 0x7ffe4c6f3d84

My pointer after edit 1-1: 
10 , 8 , 15 , 16 , 23 , 42 ,

In the first for loop, note that you are incrementing the pointer (mypointer++) AFTER reading the value.

Note that after printing the last value, 42, you increment the pointer. If you are already at the last position and increment the pointer, it’s going to point to an address that it’s not any address of
myarray. That is why if you don’t do “mypointer = myarray ;”, doing “*mypointer = 10;” the second time is not going to modify the array.

By the way, your code is not secure, because in the last iteration you going to point to an address that you may not have the right to access, making your program crash.

A possible better way to change mypointer would be to use the index, as in the example below:

#include <iostream>


int main() {
    int myarray[6] = {4,8,15,16,23,42};
    int *mypointer;
    mypointer = myarray ;
    std::cout << std::endl << "My pointer after edit 1:" << std::endl;
    *mypointer = 10;

    // -----------------------------------
    // 🔥️🔥️🔥️ YOUR WAY 🔥️🔥️🔥️
    // -----------------------------------
    for (int i = 0; i < 6; i++){
        std::cout << *mypointer << " at " << mypointer << std::endl;
        mypointer++;
    }

    mypointer = myarray ;
    std::cout << std::endl << "My pointer after edit 1-1: " << std::endl;
    *mypointer = 10;
    for (int i = 0; i < 6; i++){
        std::cout << *mypointer << " , ";
        mypointer++;
    }

    // -----------------------------------
    // 🔥️🔥️🔥️ A POSSIBLE SAFER WAY 🔥️🔥️🔥️
    // -----------------------------------
    std::cout << "\n\n SAFER WAY: " << std::endl;
    for (int i = 0; i < 6; i++){
        mypointer = &myarray[i];
        std::cout << *mypointer << " at " << mypointer << std::endl;
    }

    std::cout << std::endl << "NOW CHANGING *mypointer AND PRINTING IT" << std::endl;
    *mypointer = 70;
    std::cout<<"*mypointer: " << *mypointer << std::endl;
    std::cout<<"myarray[5]: " << myarray[5] << std::endl;


    return 0;
}

which prints:

My pointer after edit 1:
10 at 0x7ffdc5d3a4d0
8 at 0x7ffdc5d3a4d4
15 at 0x7ffdc5d3a4d8
16 at 0x7ffdc5d3a4dc
23 at 0x7ffdc5d3a4e0
42 at 0x7ffdc5d3a4e4

My pointer after edit 1-1: 
10 , 8 , 15 , 16 , 23 , 42 , 


 SAFER WAY: 
10 at 0x7ffdc5d3a4d0
8 at 0x7ffdc5d3a4d4
15 at 0x7ffdc5d3a4d8
16 at 0x7ffdc5d3a4dc
23 at 0x7ffdc5d3a4e0
42 at 0x7ffdc5d3a4e4

NOW CHANGING *mypointer AND PRINTING IT
*mypointer: 70
myarray[5]: 70

I hope you now understand.

If you don’t understand it yet, please read again carefully what I wrote.

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.