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.