Pointers

Wednesday, 10 July 2013
C++ POINTERS

Concept of Pointers:

Every storage location of memory has an associated address. Address is a number that grows sequentially. For every program placed in memory, each variable or function in the program has an associated address.

The address of operator:
The address of operator or Reference operator is denoted by the notation &. When the user wants to get the address of a variable, then the reference operator & can be used. The operator & is used to find the address associated with a variable.
The syntax of the reference operator is as follows:
&variable name
This means that the address of the variable name is achieved.
For Example


#include <iostream.h>
void main()
{
int exf=200;
int test=300;
cout<<endl<<&exf
<<endl<<&test;
}

The output of the above program is:
0x92343456
0x78252348
The&exf has the address associated with the integer variable exf and the &test has the address associated with the integer variable test which are displayed using the cout statement.
Using the understanding of address of operators, the discussion turns to the concept of pointers.

Item = 100;
test = item;
x = &item;
Using the above information, the assignment takes place as below:


Item is an integer variable having the value of 100 stored in memory address location 3501.
When the variable item is assigned to the variable test in the second statement:
Test = item;
The value of the variable item 100 is copied to the variable test.
In the third statement, the address of the variable item is denoted by reference operator &item is assigned to the variable x as:
x = &item;
The address of the variable 3501 and not the contents of the variable item are copied into the variable x. The pointers concept fits in this statement. Pointers are the variable that stores the reference to another variable. Pointers are variables that store the address of the variable that it is pointed by. Variable x is referred to as the pointer in the above example.
The programmer must note that the address operator placed before a variable is not the same as operator & placed after the variable. For example, &x is not same as x&. Variable &x refers to address operator whereas x& refer to reference operator&.
Pointer is a variable that holds the address, also called pointer variable.

Defining Pointer Variables or Pointer:
In order to define pointer variables, the programmer must use the operator denoted as * in C++.
The symbol * when placed before a pointer, variable means that it as a pointer to.
While defining variables, the data type is placed before it. When the programmer wants to define the integer variable i it is written:
Int i;
A programmer may think that to define pointer variable there is a separate data type. But this is not the case. There is no separate data type for pointer available. When a programmer defines a pointer variable, he or she can point to integer, float, and char. The compiler must know the type of data the pointer is pointing to.
To define pointer variable is as follows:
datatype_of_ variable_pointedto* pointer_varaible;
For example:
Char* chi;
This defines that ch is a pointer variable which points to char data type.
Int* i;
This defines that i is a pointer variable which points to int data type.
Float* f;
This defines that f is a pointer variable which points to float data type.

C++ Void Pointer and Null Pointer

Pointer to Void
General Syntax:
Void* pointer variable;
Void is used as a keyword.
Referring back to pointer definitions and usage, it is known that the data type the pointer variable defines is the same as the data type the pointer points to. The address placed in a pointer must have the same type as the pointer.
For example:


int i;
float f;
int* exf;
float* test;
then
exf=&i;

Is correct because the address of integer variable is stored in an integer pointer.
If a user writes the statement:
Exf=&f;
Then this statement produces an error. The address of the float variable is stored in an integer pointer that is incorrect.
Similarly, if the programmer tries to place the address of an integer variable to a float pointer, such as:
Test=&i;
The above statement will also show an error.
The Pointer to Void is a special type of pointer that the programmer can use to point to any data type.
Using the above example, the programmer declares pointer to void in this manner:
Void* sample;
Using the above example’s definition and assigning the pointer to void to the address of an integer variable is perfectly correct.
Sample=&i;
Using the above example to define the pointer to void and assign the pointer to void to the address of a float variable as below is also perfectly correct.
Sample=&f;
Pointer to void, or a void pointer, is a special type of pointer that has a great facility of pointing to any data type. There are limitations in the usage of void pointers that are explained below.
The concept of dereferencing using the operator * has been explained in an earlier section of this tutorial. The programmer must note that void pointers cannot be de-referenced in the same manner. Direct dereferencing of void pointer is not permitted. The programmer must change the pointer to void as any other pointer type that points to valid data types such as, int, char, float and then dereference it. This conversion of pointer to some other valid data type is achieved by using the concept of type-casting (refer to type-casting section of this tutorial).

NULL Pointer:

The concept of NULL pointer is different from the above concept of void pointer. NULL pointer is a type of pointer of any data type and generally takes a value as zero. This is, however, not mandatory. This denotes that NULL pointer does not point to any valid memory address.
For example:
int* item;
item=0;
The above statement denotes item as an integer pointer type that does not point to a valid memory address. This shows that item has a NULL pointer value.
The difference between void pointers and NULL pointers:
A Void pointer is a special type of pointer of void and denotes that it can point to any data type. NULL pointers can take any pointer type, but do not point to any valid reference or memory address. It is important to note that a NULL pointer is different from a pointer that is not initialized.
For example, if a programmer uses the program below:


#include <iostream.h>
int *item;
void main()
{
  *item=100;
}

The output of the above program is

NULL POINTER ASSIGNMENT
The above program will result in a runtime error. This means that the pointer variable item is not assigned any valid address and, therefore, attempting to access the address 0 gives the above error message.

No comments:

Post a Comment