+ 1
a pointer is a variable that holds a memory address as its value. To declare a point to an integer just use 'int *ptr;'. Note the asterisk here is not a dereference. Its part of the pointer declaration syntax.
After the start of your program it lies on the RAM, ie your program gets a own addressspace by the operation system. This means each variable in your code has an address.
Lets do an example:
int main(){
int value = 5; //value lies on the stack of your program and has the address...
cout<<&value<<endl;
int *ptr= &value; //initialize ptr with the address of the variable
cout<<"Address of variable ptr: "<<&ptr<<"\n"
<<"Data of the variable ptr, ie pointer holding address: "<<ptr<<"\n"
<<"get the data of that address: "<<*ptr<<"\n";
return 0;
}
But there are some restrictions for the stack of your program. It can only hold 4-8Mb of memory. This means something like 'int ary[10000000]{0};' within your code isnt possible, i.e. you get an allocation error. On the other hand, sometimes you dont no how large your datastruture is or you dont no how many elements you need. To overcome this problems pointers are a good solution because we only need to know the entrence point, ie. the first adress of a dynamic datastruture. Furthermore, all this data (memory) lies on the heap which can grow up to several Gb depending on the size of the RAM in your machine. But you can use pointers also to call functions.