Can I use a pointer to change data saved at a memory location?

Hi

I am new to C++ and I'm trying to understand pointers. I have researched and understood the general idea, what they are and how to use them etc.

My question is, if I can declare a pointer to store a memory location of another variable can I then use that information to change the data that is stored at that particular memeory location?

If it is possible, how can I do that?

Thanks

Alex
Yes. Pointers would be significantly less powerful if you wouldn't be able to do anything with them.

1
2
3
4
5
6
7
8
int main() {

	int i = 10;
	int* iptr = &i;//pointer to i

	*iptr = 20;//object pointed to by iptr is now 20

}
Yes.
1
2
3
4
int x = 6; //Declare variable and store 6 in it
int* ptr = &x; //Declare pointer pointing at memory area x occupied
*ptr = 1; //Write 1 to the memory area ptr pointing at
std::cout << x; //output bvalue of x 
Last edited on
Thank you! Just wanted to clarify as I wasn't sure If I undersood it right. :)
Topic archived. No new replies allowed.