pointers

I dont understand about pointers and how to code in pointers?
@CHIDYAONGA

As you can imagine, this is a monster question, which means a question that is too broad to be answered in a meaningful way in a forum. The first, only and obvious advice is... study! From a book (better) or a tutorial or ...
Could you ask a slightly more specific question?

A pointer as essentially a number: a memory address. It can be dereferenced (assuming it's a valid memory location that your program has access to) which gets you access to the data stored there.

1
2
3
4
5
6
7
8
9
int a;   // create a variable named "a" on the stack that is large enough to hold an int.
int *p;  // create a variable named "p" on the stack that is large enough to hold a memory address.
p = &a;  // store the memory address of the local (stack) variable "a" into "p".
cout << "The memory address of a is " << p << '\n';

p = new int;  // allocate enough memory on the heap to hold an int, and store that memory address on the stack in "p".
*p = 200;     // store the value 200 on the heap by dereferencing the memory address stored in p.
cout << "The value " << *p << " is now stored at location " << p << " on the heap.\n";
delete p;     // deallocate the heap memory previously allocated, so the OS knows it is free. 
Last edited on
I would help us if you explained to us exactly what you don't understand about pointers.
Topic archived. No new replies allowed.