Pointers Notes Class 12 Computer Science
10, 20, 30, 40, 50,
10, 20, 30, 40, 50,
Note: A pointer variable can be used as an array as in above example both *p and p[0] refers to the same variable similarly b[0] and *b refers to the same variable. Again p[1] and *(p+1) are same.
Address calculation method is same for both pointer and array. For example int a[5]={2,4,6,7,9};
int *q=a;
The address of a[index] is calculated as
Base address of a (i.e. address of a[0]) + index * sizeof (data type of a)
for example if the base address of a is 1000 then address of a[3] is calculated as
&a[3] =1000 + 3 * sizeof (int) = 1000 + 3 * 2=1000 + 6 = 1006
Note that a[3] and *&a[3] both will give the same value i.e. 7
Similarly address of (q+3) is calculated as
Address stored in q + 3 * sizeof (data type of pointer belongs to in this case int) (q+3) = 1000 + 3 * sizeof (int) = 1000 + 3 * 2=1006
Arithmetic operation on pointers
We can increment or decrement a pointer variable for example float a[5]={3.0,5.6,6.0,2.5,5.3};
float *p=a;
++p;
–p;
if the base address of a is 1000 then statement ++p will increment the address stored in pointer variable by 4 because p is a pointer to float and size of a float object is 4 byte, since ++p is equivalent to p=p+1 and address of p+1 is calculated as
Address stored in p + 1 * sizeof (float)= 1000 + 1 * 4 = 1004.
We can add or subtract any integer number to a pointer variable because adding an integer value to an address makes another address e.g.
void main()
{int p[10]={8,6,4,2,1}; Int *q;
q=p;//address of p[0] is assigned to q assuming p[0] is allocated at memory location 1000 q=q+4;//now q contains address of p[4] i.e. 1000+4*sizeof(int)=1000+4*2= 1008
cout<<*q<<endl;
q=q-2;//now q contains address of p[2] i.e. 1008-2*sizeof (int)=1008-2*2=1004 cout<<*q<<endl;
}
Output is
1
4
Addition of two pointer variables is meaningless.
Subtraction of two pointers is meaningful only if both pointers contain the address of different elements of the same array
For example
void main()
{int p[10]={1,3,5,6,8,9};
int *a=p+1,*b=p+4;
p=p+q; //error because sum of two addresses yields an illegal address int x=b-a; //valid
cout<<x;
}
Output is
3
In the above example if base address of a is 1000 then address of a would be 1002 and address of b would be 1008 then value of b-a is calculated as
(Address stored in b-address stored in a)/sizeof(data type in this case int) (1008-1002)/2=3
Multiplication and division operation on a pointer variable is not allowed
We cannot assign any integer value other than 0 to a pointer variable. For example
int *p;
p=5; //not allowed
p=0; //allowed because 0 is a value which can be assigned to a variable of any data type
Null Pointer
A null pointer is a regular pointer of any pointer type which has a special value that indicates that it is not pointing to any valid reference or memory address. This value is the result of type-casting the
integer value zero to any pointer type.
int *q=0; // q has a NULL pointer value float * p;
p=NULL; // p has a NULL (NULL is defined as a constant whose value is 0) pointer value
Note: 0 memory address is a memory location which is not allocated to any variable of the program.
voidpointers
void pointer is a special pointer which can store address of an object of any data type. Since void pointer do not contain the data type information of the object it is pointing to we can not apply dereference operator * to a void pointer without using explicit type casting.
void main()
{
int x=5;
float y=3.5;
int *p;
float *q;
void *r;
p=&y; //error cannot convert float * to int * q=&x; //error cannot convert int * to float * p=&x; //valid
cout<<*p <<endl; //valid q=&y; //valid
cout<<*q<<endl ; //valid
r=&x; //valid
cout<<*r<<endl; //error pointer to cannot be dereference without explicit type cast cout<<*(int *)r<<endl; // valid and display the values pointed by r as int
r=&y; //valid
cout<<*(float *)r<<endl; //valid and display the values pointed by r as float
}
Note: Pointer to one data type cannot be converted to pointer to another data type except pointer to void
Array of pointers and pointer to array
An array of pointer is simply an array whose all elements are pointers to the same data type.
For example
Void main()
{
float x=3.5,y=7.2,z=9.7;
float *b[5]; // here b is an array of 5 pointers to float b[0]=&x;
b[1]=&y;
b[2]=&z;
cout<<*b[0]<<”\t”<<*b[1]<<”\t”<<*b[2]<<endl;
Discover more from EduGrown School
Subscribe to get the latest posts sent to your email.