With pointers we can perform lot many operations by using memory addresses. It is possible to access and display the memory location of a variable using '&' operator. A pointer variable holds the memory address of any type of variable. The pointer variable and normal variable should be of the same type. The pointer is denoted by asterisk (*) symbol.
Pointer: A pointer is a variable that holds a memory address. A pointer can have any name that is legal for other variable, and it is declared in the same fashion like other variables but it is always declared by * operator.
When a variable of any data type is declared, according to its data type the memory is reserved.
As shown in Figure 8.1, the memory locations are reserved for the variables x, y and c. Integer variables require two bytes each and for character variable one byte is reserved. The locations 65550 and 65551 are used to store ‘x’, 65552 and 65553 are used to store ‘y’ and so on. The starting address for ‘x’ is 65550 and ‘y’ is 65552. The character needs only one byte and its location shown in Figure 8.1 is 65554.
FIGURE 8.1 Memory allocated
The pointer variables can be declared as follows.
Example:
int *x;
float *f;
char *y;
Arithmetic operations on pointer variables are also possible. Increment, decrement, prefix and postfix operations can be performed with the pointers. The effect of these operations is shown in Table 8.1 given below.
From the above table, we can observe that on the increment of pointer variable for integers, the address is incremented by 2, i.e. 4046 is the original address and on increment its value will be 4048, because integers require two bytes.
Similarly, for characters, floating point numbers and long integers require 1, 4 and 4 bytes, respectively. After the effect of increment and decrement, the memory locations are shown in Table 8.1. Refer program number 14 under 8.E for detail understanding.
TABLE 8.1 Pointer and arithmetic operation
The array name itself is an address or pointer. It points to the address of the first element (0th) element of an array). The elements of the array together with their addresses can be displayed by using array name itself. The array elements are always stored in contiguous memory locations.
A pointer is known as a variable containing address of another variable. The pointer variables also have an address. The pointer variable containing address of other pointer variables is called as pointer to pointer. This chain can be continued to any extent.
There is a technique by which program can obtain space in the main memory which is called dynamic allocation. Dynamic allocation is a technique in which a program can acquire storage space in the main memory. In this method, the space for the program is allocated during execution of the program. The free region of the memory is called the heap.
pnt=(data type*)malloc(given size);
Here, from the data type, compiler understands the pointer type, and given size is the size to reserve the memory.
pnt=(int *)malloc(20);
Here, in this declaration, 20 bytes are allocated to pointer variable pnt of type int and base address is returned to pointer pnt.
pnt=(int *)calloc(4,2);
The above declaration allocates four blocks of memory; each block contains 2 bytes. The base address is stored in the integer pointer. This function is usually used for allocating memory for array and structure. The calloc() can be used in place of malloc() function. The programs illustrated with malloc()function can be executed using calloc() function.
free(pnt);
In the above declaration, pnt is a pointer. The free() function releases the memory occupied by the pointer variable pnt.
pnt=realloc(pnt,newsize);
This function reallocates new memory of new size to the pointer variable pnt.
8.A Fill in the blanks
8.B True or false
8.C Match the following correct pairs given in Group A with Group B
8.D Select the appropriate option from the multiple choices given below
8.E What will be the output/s of the following program/s?
void main()
{
int num=10,*p;
p=#
clrscr();
printf(" %d",*p);
printf(" %d",num);
printf(" %u",&num);
printf(" %u",p);
getche();
}
Explanation: Pointer ‘p’ points value stored at address of num. To access the value of num, we use pointer. Value of num = 10. In the first two printf() statements, the variable value 10 is printed. Also, in the third and fourth printf() statements, the addresses of num are displayed.
void main()
{
int p=10;
void *pt=&p;
clrscr();
*(int*)pt=12;
printf("%d",p);
}
Explanation: Void pointer holds the address of any data type such as int, float, char and by assigning the address, we can typecast it, i.e. which data type. The *(pt) points the value stored at the address held by pt. The next value assigned to the pointer is 12 and the same is displayed.
void main()
{
int t=10,*p;
clrscr();
p=&t;
printf("
t = %d t = %d t = %d",
t,*(&t),*p);
}
Explanation: In the above program, it displays the value of the variable ‘t’. Three different syntaxes are used in the printf() statement.
void main()
{
long int a,*pa;
float b,*pb;
clrscr();
printf("Enter Integer & float Value :");
scanf("%ld %f",&a,&b);
pa=&a;
pb=&b;
printf("
Address of a=%u",pa);
printf("
Value of a=%ld",a);
printf("
Value of a=%ld",*(&a));
printf("
Value of a=%ld",*pa);
printf("
Address of b=%u",pb);
printf("
Value of b=%.2f",b);
printf("
Value of b=%.2f",*(&b));
printf("
Value of b=%.2f",*pb);
}
Explanation: Integer value 2345 and float value 2.345 are prompted through the keyboard. The same values and their addresses are printed on the screen.
void main()
{
clrscr();
printf("char %d byte & its pointer %d bytes
",
sizeof(char), sizeof(char*));
printf("int %d byte & its pointer %d bytes
",sizeof(int),sizeof (int*));
printf("float %d byte & its pointer %d bytes
",sizeof(float),sizeof (float*));
}
Explanation: Pointer of any data type requires two bytes. This is shown in all the three printf() statements.
void main()
{
char c[]="India";
char *p;
p=c;
clrscr();
printf("%s",p);
}
Explanation: Here, the address of 0th element is assigned to character pointer ‘p’. In other words, base address of the string is assigned to ‘p’. After every increment of ‘p’, the pointer goes to the next character of the string. The displayed string on the screen is India.
void main()
{
char *c="India";
char *p ="Bharat";
char *k;
k=p;
p=c;
clrscr();
printf("%s",p);
printf(" %s",k);
}
Explanation: Here, the address of the 0th element is assigned to character pointers ‘c’ and ‘p’. In other words, base addresses of a string are assigned to ‘k’ and ‘p’. The displayed string on the screen is India and Bharat.
Arithmetic operations with pointers
void main()
{
int a=5,b=7,c,d,*ap,*bp;
clrscr();
ap=&a;
bp=&b;
c=a+b;
d=*ap+*bp;
printf("
Sum of A & B :%d",c);
printf("
Sum of A & B : %d",d);
}
Explanation: Here, sum of A and B is 12 .With simple arithmetic addition a+b, which is stored in ‘c’, is displayed sum of A & B = 12, i.e. addition of value pointed by pointer ap & bp is 12.
void main()
{
int a=25,b=10,*p,*j;
p=&a;j=&b;
clrscr();
printf(" %d %d %d %d %d",*p+b, *p–b,*p * *j,*p / *j,*p % *j);
}
Explanation: Execution of printf() always starts from right to left, so in following sequences operation will be performed:
*p%*j = 25 % 10 = 5
*p / *j = 25 /10 = 2
*p * *j = 25 * 10 = 250
*p-b = 25 – 10 = 15
*p+b = 25+10 = 35
Thus, the output will be as 35, 15, 250, 2, 5
void main()
{
int num=10,*p;
p=#
clrscr();
num=*p**p+*p+*p;
printf(" %d",num);
getche();
}
Explanation: Here, in this program *p = 10 & num = 10, hence num=*p * *p + *p + *p=10*10+ 10+10= 120
void main()
{
char name[15]="He",*ch;
ch=name;
clrscr();
while(*ch!=' ')
{
printf("%c",(*ch)+1);
ch++;
}
}
Explanation: *ch points character stored in name array and gives its value in ASCII by %c, thus it will print "He". But (*ch)+1 indicates ASCII value added by 1 every time, hence instead of He it will print If [(I=(ASCII of H)+1) & (f=(ASCII of e)+1)]
void main()
{
char name[15]="Good",*ch;
ch=name;
clrscr();
while(*ch!=' ')
{
printf("%c",(*ch)-1);
ch++;
}
}
Explanation: The answer is also similar to previous one.
void main()
{
int x[5]={3,4,6,8,10},t=0;
clrscr();
printf("
Element No. Element Address");
while(t<5)
{
printf("
x[%d] = %8d %9u",t,*(x+t),x+t);
t++;
}
}
Explanation: In this program, initially t=0 & *(x+t)=> x[t] && x+t=>&x increments with positions indicated by ‘t’. Thus, elements and addresses are displayed.
void main()
{
int num=10,*p;
clrscr();
p=#
printf(" %u",p);
printf(" %u",p+4);
printf(" %u",p-2);
printf(" %d",++(*p));
getche();
}
Explanation: The ‘p’ prints the address of num and it is 65498. p+4 mean base address added by 8 bytes, i.e. (65498+8=65506) and p-2 becomes (65498–4=65494) and at last ++((*p))=++(10)=11.
Array operations and display
void main()
{
int x[]={1,2,3,4,5,6,7},k=1;
clrscr();
while(k<=5)
{
printf("%2d",k[x-1]);
k++;
}
Explanation: Initially k=1 & k[x-1] indicates & [(base address)–one location]=first address, now ‘k’ will increment the position of base address by particular location, but ‘k’ will increment up to 5 only, the output will print up to 5 value from 0th location to 4th location.
void main()
{
int a[]={15,20,40},b[3],j=0,k=2;
clrscr();
for(j=0;j<3;j++)
{
b[j]=a[k];
k--;
}
j=0;
while(j<3)
printf(" %d ",b[j++]);
}
Explanation: Inner for loop will assign value as
b[0]=a[2]=>b[0]=40
b[1]=a[1]=>b[1]=20
b[2]=a[0]=>b[2]=15
thus output will of b[0],b[1],b[2] as 40,20,15.
void main()
{
int arr[]={10,20,30},p=0;
clrscr();
for(p=0;p<3;p++)
{
printf("
%d %d %d",
p[arr],*(arr+p),arr[p]);
}
}
Explanation: In this program p[arr]=>arr[p].*(arr+p)=> pointer points the base address and holds the value stored in it and position increments by ‘p’ value.
arr[p] will print
0[arr]=*(arr+0)=arr[0]=10
1[arr]=*(arr+1)= arr[1]=20
2[arr]=*(arr+2)=arr[2]=30
void main()
{
int arr[]={15,20,40},sum=0,j=0;
clrscr();
while(j<3)
sum=sum+*(arr+j++);
printf("
sum %d",sum);
}
Explanation: Initially j=0 and the loop will execute thrice as
sum=0+*(arr+0)=0+15=15
sum=15+*(arr+1)=15+20=35
sum=35+*(arr+2)=35+40=75
void main()
{
int *arr[3];
int ar[]={5,10,15},k;
for(k=0;k<3;k++)
arr[k]=ar+k;
clrscr();
for(k=0;k<3;k++)
printf("%d ",*(arr[k]));
}
Explanation: *arr[ ] is array of pointer. The arr[k]=ar+k holds the address and pointer points the value stored in that address.
#include <math.h>
void main()
{
int k[]={1,2,3,4,5},t,sumsq=0, sumc=0;
clrscr();
for(t=0;t<5;t++)
{
sumsq=sumsq+pow(*(t+k),2);
sumc=sumc+pow(k[t],3);
}
printf("
Sum of Squares of array elements : %d", sumsq);
printf("
Sum of Cubes of array elements : %d",sumc);
}
Explanation:
pow(k[0],2)+pow(k[1],2)+pow(k[2],2)+ pow(k[3],2)+pow(k[4],2)=pow(1,2)+ pow(2,2)+pow(3,2)+pow(4,2)+pow (5,2)=1+4+9+16+25=55
pow(k[0],3)+pow(k[1],3)+pow (k[2],3)+pow(k[3],3)+pow(k[4],3)=pow(1,3)+pow(2,3)+pow(3,3)+pow(4,3)+pow(5,3)= 1+8+27+64+125=225
void main()
{
int i,j=1,*p;
int a[3][3]={{1,2,3},{4,5,6}, {7,8,9}};
clrscr();
printf(" Elements of An Array with their addresses
");
p=&a[0][0];
for(i=0;i<9;i++,j++)
{
printf("%5d [%5u]",*(p),p);
p++;
if(j==3)
{
printf("
");
j=0;
}}
}
Explanation:
%5d indicates identifier for integer value with 4 blank spaces.
%5u indicates identifier for integer address.
p holds the base addresses which are always indicated by a[0][0] and pointer points value at that address.
Thus,*p gives pointed values after incrementing its location as p++ by 2 bytes.
Elements and their addresses are displayed.
void main()
{
int so[]={1,2,3,4,5},*pb,ds[5],i;
pb=so;
clrscr();
for(i=0;i<5;i++)
{
ds[i]=*pb;
pb++;
}
printf("Original Array Duplicated Array");
for(i=0;i<5;i++)
printf("
%d %d", so[i],ds[i]);
}
Explanation: In this program, ‘pb’ holds the address of ‘so’and *pb points value at that address which is assigned to ‘ds’.
void main()
{
int *arrp[3];
int arrl[3]={5,10,15},k;
for(k=0;k<3;k++)
arrp[k]=arrl+k;
clrscr();
printf("
Address Element
");
for(k=0;k<3;k++)
{
printf(" %u",arrp[k]);
printf(" %7d
",*(arrp[k]));
}
}
Explanation: The arrp is array of pointer, i.e. arrp[0], arrp[1], arrp[2] will give address of each element.
arrl is integer array which has value 5 10 15.
"arrp[k]=arrl+k" indicates assigning the address to array pointer which points the value stored at that address.
Pointer to pointer
void main()
{
int a=2,*p,**q;
p=&a;
q=&p;
clrscr();
printf("%d %d %d",a,*p,**q);
}
Explanation:
void main()
{
int k;
int a[]={1,2,3};
int *b[3];
int **c[3];
int ***d[3];
int ****e[3];
int *****f[3];
clrscr();
for(k=0;k<3;k++)
{
b[k]=a+k;
c[k]=b+k;
d[k]=c+k;
e[k]=d+k;
f[k]=e+k;
}
for(k=0;k<3;k++)
{
printf("%3d",*b[k]);
printf("%3d",**c[k]);
printf("%3d",***d[k]);
}
}
}
printf("%3d",****e[k]);
printf("%3d
",*****f[k]);
}
}
Explanation:
*b[3] is array pointer of size 3.
**c[3] is array pointer to pointer of size 3.
***d[3] is array pointer to pointer to pointer of size 3.
****e[3] is array pointer to pointer to pointer to pointer of size 3.
*****f[3] is array pointer to pointer to pointer to pointer to pointer of size 3.
b[k]=a+k; ‘b’ holds the address of ‘a’.
c[k]=b+k; ‘c’ holds the address of ‘b’.
d[k]=c+k; ‘d’ holds the address of ‘c’.
e[k]=d+k; ‘e’ holds the address of ‘d’.
f[k]=e+k; ‘f’ holds the address of ‘e’.
Double pointers
void main()
{
int a=2,*p,**q;
p=&a;
q=&p;
clrscr();
printf ("
Value of a= %d Address of a=%u",a,&a);
printf ("
Through *p Value of a= %d Address of a=%u",*p,p);
printf ("
Through **q Value of a= %d Address of
a=%d",**q,*q);
}
Explanation:
The address of a becomes –40.
It takes value within the range –128 to +127, thus *q holds the address of a=65496–65536=–40.
If we take %u, then it prints the address of a=65496.
8.F Find the bug/s in the following program/s
void main()
{
float x=10.25,*f;f=x;
clrscr();
printf("%g",*f);
}
void main()
{
float x=10.25,*t,*f;
t=&x;
f=&t;
clrscr();
printf("%g",**f);
}
void main()
{
char sa[]="How are you?";
char t,*f;f=&sa;
clrscr();
printf("%s",f);
}
void main()
{
int *t,x;t=&x;x=11;
*t++;
clrscr();
printf("%d",*t);
}
void main()
{
int t[]={1,2,3,4,5};
int *p,*q,*r;p=t;q=p[1];
r=p[2];
clrscr();
printf("%d %d %d",*p,*q,*r);
}
void main()
{
int num[2][3]={1,2,3,4,5},*k;
k=#
clrscr();
printf("%d",*k);
}
void main()
{
int x=5,y=8,z;
int *px,*py,*pz;px=&x;py=&y;pz=&z; pz=*px+*py;
clrscr();
printf("%d",z);
}
void main()
{
int x=5;
int *px=&x,*py;
clrscr();
printf("%d",px);
}
void main()
{
int x,*px;
*px=5;
px=&x;
clrscr();
printf("%d",x);
}
void main()
{
int *p,*q,a=2;
p=&a;
q=p;
clrscr();
printf("%d %d", p, q);
}
void main()
{
int *p,*q,**t,a=2;
p=&a;
q=p;
t=p;
clrscr();
printf("%d %d %d", *p, *q, **t);
}
void main()
{
int y[]={1,2,3},*p;
p=&y[1];
clrscr();
printf("%d %d",(1+y),*p);
}
void main()
{
unsigned y[]={65555,65550,65543};
clrscr();
printf("%u",y[2]);
}
void main()
{
int p[]={1,2,3,4};
char *c;
c=p;
clrscr();
printf("%d",*c);
}
8.G Attempt the following programming exercises
8.A Fill in the blanks
4054 | 4052 |
4062 | 4054 |
4064 | 4056 |
8.B True or false
1. T | 2. T | 3. T | 4. T | 5. T |
6. T | 7. F | 8. F | 9. F | 10. F |
11. T | 12. F | 13. T | 14. T |
8.C Match the following correct pairs given in Group A with Group B
8.D Select the appropriate option from the multiple choices given below
8.E What will be the output/s of the following program/s?
Address of a=65492
Value of a=2345
Value of a=2345
Value of a=2345
Address of b=65496
Value of b=2.35
Value of b=2.35
Value of b=2.35
int 2 byte & its pointer 2 bytes
float 4 byte & its pointer 2 bytes
Sum of A & B : 12
x[0] = 3 65490
x[1] = 4 65492
x[2] = 6 65494
x[3] = 8 65496
x[4] = 10 65498
20 20 20
30 30 30
Sum of Cubes of array elements : 225
1[65482] 2[65484] 3[65486]
4[65488] 5[65490] 6[65492]
7[65494] 8[65496] 9[65498]
1 1
2 2
3 3
4 4
5 5
65494 5
65496 10
65498 15
2 2 2 2 2
3 3 3 3 3
Through *p Value of a=2 Address of a=65496
Through **q Value of a=2 Address of a=–40
8.F Find the bug/s in the following program/s
18.224.229.142