C++ Pointers

Pointers :
A pointer is a variable depending on its type that may hold / contain the memory address of another variable. When a variable is declared, three(3) attributes are associated with it, its name, type and memory address. The memory address is in hexadecimal notation see picture.

There are two pointer operators in C++: & The “address of” or referencing operator. * The dereferencing operator

Declaring Pointers
Declare all pointers before using them, the best way to begin learning about pointers is to understand how to declare and define them. Actually, declaring pointers is almost as easy as declaring regular variables. After all, pointers are variables. If you must declare a variable that holds your n, you could do so with the following variable declaration: int n=66; // Declare a variable to hold number n. Declaring n like this does several things. It enables C++ to identify a variable called n, and to reserve storage for that variable. Using this format also enables C++ to recognize that you will store only integers in n, not floating-point or double floating-point data. The declaration also requests that C++ store the value of 30 in n after it reserves storage for n. Where did C++ store n in memory? As the programmer, you should not really care where C++ stores n. You do not have to know the variable’s address because you will never refer to n by its address. If you want to calculate with or print n, you call it by its name, n. Example: //display the value int main { int n = 66; cout<<n<<endl; return 0; } Suppose you want to declare a pointer variable. This pointer variable will not hold your number, but it will point to n, the variable that holds your number. p might be a good name for the pointer variable. The figure assumes C++ stored n at the address 000ff31. Your C++ compiler, however, arbitrarily determines the address of n, so it could be anything. To declare the p pointer variable, you must program the following: int * p; // Declares an integer pointer. Similar to the declaration for n, this declaration reserves a variable called p. The p variable is not a normal integer variable, however. Because of the dereferencing operator, *, C++ knows this is to be a pointer variable. Some C++ programmers prefer to declare such a variable without a space after the *, as follows: int *p; // Declares an integer pointer. Remember the * is not part of the name. When you later use p, you will not prefix the name with the *, unless you are dereferencing it (accessing the value of the variable) at the time. The * is important, because it tells C++ to interpret p as a pointer variable, not as a normal, data variable.
 * 1) include

Assigning Values to Pointers p is an integer pointer. This is very important. p can point only to integer values, never to floating-point, double floatingpoint, or even character variables. If you needed to point to a floating-point variable, you might do so with a pointer declared as  float *point; // Declares a floating-point pointer. As with any automatic variable, C++ does not initialize pointers when you declare them. If you declared p as previously described, and you wanted p to point to n, you would have to explicitly assign p to the address of n. The following statement does this: p = &n; // Assign the address of n to p. What value is now in p? You do not know exactly, but you know it is the address of n, wherever that is. Rather than assign the address of n to p with an assignment operator, you can declare and initialize pointers at the same time. These lines declare and initialize both n and p: int n=66; // Declares a regular integer // variable, putting 66 in it. int *p=&n; // Declares an integer pointer, // initializing it with the address // of p. If you wanted to output the value of n, you could do so with the following cout: cout << n; // Prints the value of n. You can also output the value of n like this: cout << *p; // Dereferences p. The dereference operator produces a value that tells the pointer where to point. Without the *, the last cout would print an address (the address of n). With the *, the cout prints the value at that address. You can assign a different value to n with the following statement: n=41; // Assigns a new value to n. You also can assign a value to n like this: *p=41; This declaration assigns 41 to the value to which p points. new Operator: This is another way of declaring apointer. It is done by use of the key word new as follows: Assume you want to create a pointer called p that holds a value of 3.14159 float *p; p = new float; p* = 3.14159; This can also be written as: float *p = new float; p* = 3.14159; Or also written as: float *p = new float(3.14159); Example: #include	int main {	//create a pointer float *p = new float; p* = 3.14159; cout<<*p<	int main {	//create a pointer float *p = new float; p* = 3.14159; delete p;		p* = 22.0;// Pointer already destroyed error message cout<<*p<<endl; return 0; }

Reference:
A variable is a symbol that represents a storage location in the memory of a computer. Reference is another name for a variable, it is also known as an alias or a synonym and is represented by the symbol &. //display the value int main { int score = 60; cout<<score<<endl; int &marks = score; cout<<marks<<endl; return 0; } Example of pointers: // Filename: POINT.CPP // Demonstrates the use of pointer declarations // and operators. void main {  int number=123; // A regular integer variable. int *poi_number; // Declares an integer pointer. cout << “number is “ << number << “\n”; // Outputsvalue of number. cout << “The address of number is “ << &number << “\n”; // Outputs number’s location. poi_number = &number; // Puts address of number in poi_number, // in effect making poi_number point // to number. // No * in front of poi_number. cout << “*poi_number is “ << *poi_number << “\n”; // Prints value // of number. cout << “poi_number is “ << poi_number << “\n”; // Prints location // of number. return; }
 * 1) include
 * 1) include 

Pointers and Parameters
With the knowledge of * and & operators, you can finally see why scanf’s requirements were not as strict as they first seemed. While passing a regular variable to scanf, you have to prefix the variable with the & operator. For instance, the following scanf gets three integer values from the user: scanf(“ %d %d %d”, &number1, &number2, &number3); This scanf does not pass the three variables, but passes the addresses of the three variables. Because scanf knows the exact locations of these parameters in memory (because their addresses were passed), it goes to those addresses and puts the keyboard input values into those addresses. This is the only way scanf could work. If you passed these variables by copy, without putting the “address of” operator (&) before them, scanf would get the keyboard input and fill a copy of the variables, but not the actual variables number1, number2, and number3. When scanf then returned control to your program, you would not have the input values. Of course, the cin operator does not have the ampersand (&) requirement and is easier to use for most C++ programs.

Passing Values
By default C++ passes values / parameters by copy (or “by value”). To pass by address, receive the variable preceded by an & in the receiving function. The following function receives score by address: por_it(int &score); // Receive integer score in por_it by // address (por it would normally receive // score by copy).

Example: // Filename: SWAPER.CPP // Program that includes a function that swaps // any two integers passed to it void swapper(int &num1, int &num2); void main {  int i=10, j=20; cout << “\n\nBefore swap, i is “ << i <<“ and j is “ << j << “\n\n”; swapper(i, j); cout << “\n\nAfter swap, i is “ << i <<“ and j is “ << j << “\n\n”; return; } void swapper(int &val1, int &val2) {  int temp; // Variable that holds // in-between swapped value. temp = val1; // The calling function’s variables val1 = val2; // (and not copies of them) are val2 = temp; // changed in this function. return; }
 * 1) include 

Arrays of Pointers
If you have to reserve many pointers for many different values, you might want to declare an array of pointers with each pointer being a pointer to a specific data type. int *ipointer[10]; // Reserves an array of 10 integer pointers Each element holds an address (after being assigned values) that points to other values in memory. Each value pointed to must be an integer (in this case).