KHURASAN
UNIVERSITY
Chapter 2:- Pointers
[Link]- info@[Link]
Why Pointers?
Some C++ tasks are performed more easily with pointers, and other C+
+ tasks, such as dynamic memory allocation, cannot be performed
without them
As you know every variable is a memory location and every memory
location has its address defined which can be accessed using
ampersand (&) operator which denotes an address in memory.
[Link]- info@[Link]
Reference Operator
A reference variable is a "reference" to an existing variable, and it is created
with the & operator:
string food = "Pizza"; // food variable
string &meal = food; // reference to food
Now, you can use either the variable name food or the reference name meal to
refer to the food variable:
string food = "Pizza";
string &meal = food;
cout << food << "\n"; // Outputs Pizza
cout << meal << "\n"; // Outputs Pizza
[Link]- info@[Link]
Reference Operator as Memory Address
it can also be used to get the memory address of a variable; which is the location of
where the variable is stored on the computer.
When a variable is created in C++, a memory address is assigned to the variable.
And when we assign a value to the variable, it is stored in this memory address.
To access it that address, use the & operator:
string food = "Pizza";
cout << &food; // Outputs 0x6dfed4
[Link]- info@[Link]
What is Pointer?
A pointer is a variable whose value is the address of another variable.
You have to declare a pointer variable before you can work with it.
General Syntax:
int *ip; // pointer to an integer
double *dp; // pointer to a double
float *fp; // pointer to a float
char *ch // pointer to character
[Link]- info@[Link]
Note
The actual value of all pointers, whether integer, float, character, or
otherwise, is the same, a long hexadecimal number that represents a
memory address.
The only difference between pointers of different data types is the data
type of the variable or constant that the pointer points to.
[Link]- info@[Link]
The way we use pointers in C++
1. define a pointer variable.
2. Assign the address of a variable to a pointer.
3. Finally access the value at the address available in the pointer variable
This is done by using unary operator * that returns the value of the
variable located at the address specified by its operand. This is also called
dereferencing a pointer variable.
[Link]- info@[Link]
NULL Pointer
It is always a good practice to assign the pointer NULL to a pointer
variable in case you do not have exact address to be assigned. This is
done at the time of variable declaration. A pointer that is assigned
NULL is called a NULL pointer.
The NULL pointer is a constant with a value of zero defined in several
standard libraries, including iostream.
• int *ptr = NULL; Or int *ptr = 0;
• cout << "The value of ptr is " << ptr ;
[Link]- info@[Link]
To check for a null pointer you can use an if statement as follows −
if(ptr) // succeeds if p is not null
if(!ptr) // succeeds if p is null
[Link]- info@[Link]
C++ Pointer to Pointer (Multiple
Indirection)
A pointer to a pointer is a form of multiple indirection or a chain of
pointers. Normally, a pointer contains the address of a variable.
However when we define a pointer to a pointer, the first pointer
contains the address of the second pointer, which points to the location
that contains the actual value as shown below.
[Link]- info@[Link]
Example of pointer to pointer
A variable that is a pointer to a pointer must be declared as such. This is done by placing an
additional asterisk in front of its name.
Syntax
int **var;
int var = 3000;
int *ptr;
int **pptr;
ptr = &var; // take the address of var
pptr = &ptr; // take the address of ptr using & operator
cout << "Value of var :" << var << endl;
cout << "Value available at *ptr :" << *ptr << endl;
// take the value using pptr
cout << "Value available at **pptr :" << **pptr << endl;
[Link]- info@[Link]
Passing Pointers to Functions in C++
C++ allows you to pass a pointer to a function. To do so, simply declare the
function parameter as a pointer type.
Void changeNumber(int *number){
*number = 40;
}
Main(){
Int myNumber = 30;
Cout<<“myNumber before function call: “<<myNumber<<endl;
changeNumber(&myNumber);
Cout<<“myNumber after function call: “<<myNumber<<endl;
}
[Link]- info@[Link]
END
THANK
YOU !
KHURASAN
UNIVERSITY
[Link]- info@[Link]