/* This program demonstrates how to declare, initialize and dereference pointers * A pointer is a variable that stores the address of another variable. */ #include using namespace std; int main() { int *ptr; //This is a pointer of integer type. The * operator is used to declare a pointer. int x = 5; //Regular integer varibale. ptr = &x; // The & operator indicates the address of x. // ptr now stores the address of x. //We can have multiple levels of pointers. int **q; //this is a 2 level pointer. This can store the address of a pointer q = &ptr; //q no stores the address of ptr //dereferencing the process of acessing the value stored in the address pointed to by a pointer. //The * operator, when used with a pointer in any place other than declaration, dereferences the pointer cout<<"X is "<(double) ptr; //Dereferencing works for reading in values as well //If we read a dereferenced pointer, the original variable is also affected cin>>*ptr; // x now has a new value //The null pointer is a special pointer that points to nothing. //dereferencing a null pointer will result in a segfault //NULL, null and 0 mean the same thing when assigned to a pointer q = null; //q is now null return 0; }