Pointers are variables who hold the address of other variables.
int* p; //declare an integer pointer.
// p can hold the address of an int
int x; int y; int a[10]; p = &x //p holds the address of x, p "points" to x![]()
*p = 42 //p is being dereferenced.
// assign value 42 to the place p is pointing to![]()
p = &y //p holds the address of y, p "points" to y![]()
*p = 9 //p is being dereferenced.
// assign value 9 to the place p is pointing to![]()
p = a;![]()
*p = 72;![]()
p++; //advance p to the next integer (next element in the array)
*p = 0; //Place 0 in the cell p is pointing to.
(*p)++ //increment the contents of where p is pointing to
*(p+1) = 77 //place 77 in the cell ONE cell away from p
// [I don't want to see you do this!]
Pointer arithmetics:
p += 2; or p = p + 2;
Pointer assignment, equality
p = p2 //point p2 to where p is pointing to
p == p2 //returns true if p and p2 are equal(!!): pointing to the same location
//all these variables are allocated before the program begins to run. The system // knows what they are, how big they are, where they are BEFORE the program is run int i; double d; int a[10]; int* int_ptr;
You will also notice (well, it's pretty obvoius) they all have names.
Take another look at the pointer variable:
int_ptr
this is a static variable. It's allocated at compile time. As far as allocation is concerned, there is no difference between a pointer and an int or bool.
You allocate dynamic variables using the keyword new and deallocate them using the keyword delete (or delete[] if the variable you are deallocating is an array)
int* int_ptr; //a static pointer variable. It has a name: int_ptr. // NEVER dereference or // even pass as argument an uninitialized pointer variable. int_ptr = new int; //an integer variable (with no name) was allocated on the heap. *int_ptr = 12; //the only way to access this int is through int_ptr. cout<*int_ptr; //you can use *int_ptr (dereferenced pointer) just like an int. swap(*int_ptr, b);
int_ptr++; //DON'T DO THAT! the next int is not yours to point to!!
*int_ptr = 51; //if you are going to point int_ptr somewhere else, make sure // you are not losing track of the address of your int, // or it will get lost forever. int* hold_this = int_ptr; delete int_ptr; //once you are done using this no-name int, delete it // or you will have a "leak" (Mother of all bugs.) //at this point, you have lost access to that int you created. // in fact, that int no longer exists. //dereferencing a pointer to a non-existent variable will have // catastrophic results.
int size; cout<<"how big of an array would you like? "; cin>>size;
int_ptr = new int[size]; //allocate an array of any size. The address of the array // will be stored in int_ptr. // the array itself does not have a name and the only // way to access it is through int_ptr;
for (int i=0; i<size; i++){//initializing the array. *int_ptr = 0; int_ptr++; //you just moved the pointer that pointed to the // start of your array. // How are you going to find it again? // yes, you can use pointer arithmetics to find your // way back, but i would not recommend it. // translation: DO NOT DO THIS! }
//here, you have int_pointer holding on to the start // of the array and walker, walks down the length // of the array and initializes the cells one by one. // a much better solution: Less chance of making a // mistake.
int* walker = int_ptr; for (int i = 0; i<size; i++){ *walker = 0; walker ++; //walker is now pointing to the next element of the array }
//you can use this walker to initialize, add up, print, search the array: for (int i= 0; i<size; i++){ //do something with *walker: // examin it, add it, change it...; walker++; //look at the next element of the array }
//This is how we access arrays. And ONLY this way, unless you are instructed otherwise. //The best thing you can do when you are learning pointers is to let go of your // "array thinking" Think in pointers:
while (*walker != LAST_ELEMENT){...} for (int i= 0; i<size; i++, walker++){...} ... *walker++ = 0; //set *walker to zero, (set the cell walker is // pointing to, to zero. // then (postfix) increment walker if (walker == last_position){...} //two pointers are equal if they are both // pointing to the same location
delete[] int_ptr; //deallocate the array.
// note the [ ] after delete
FALSE, FALSE, FALSE!
It's only dynamic if it was created (allocated) using the 'new' keyword