0% found this document useful (0 votes)
3 views

12.Pointers

The document provides an extensive overview of pointers in programming, including their declaration, features, and various applications such as memory management and data manipulation. It includes multiple examples of code demonstrating pointer operations, arithmetic with pointers, and their use with arrays and strings. Additionally, it explains concepts like pointers to pointers and arrays of pointers, highlighting their significance in efficient programming.

Uploaded by

Abadullah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

12.Pointers

The document provides an extensive overview of pointers in programming, including their declaration, features, and various applications such as memory management and data manipulation. It includes multiple examples of code demonstrating pointer operations, arithmetic with pointers, and their use with arrays and strings. Additionally, it explains concepts like pointers to pointers and arrays of pointers, highlighting their significance in efficient programming.

Uploaded by

Abadullah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 10

Pointers

A pointer is a memory variable that stores a memory address. 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 denoted by ‘*’ operator.

Features of pointers

1) Pointers save the memory space.


2) Execution time with pointer is faster because data is manipulated with the address i.e,
direct access to memory.
3) The memory is accessed efficiently with pointers. The pointer assigns the memory
space and it also releases. Dynamically memory is allocated.
4) Pointers are used with data structures. They are useful for representing two-
dimensional and multi-dimensional arrays.

Pointer declaration

int *x;
float *f;
char *y;

WAP to display the address of the variable.

#include<stdio.h>
#include<conio.h>
void main()
{
int num;
clrscr();
printf(“\n enter a number :”);
scanf(“%d”,&num);
printf(“\n value of num=%d\n”,num);
printf(“address of num=%u”,&num);
getche();
}
WAP to display the addresses of different variables together with their values.

#include<stdio.h>
#include<conio.h>
void main()
{
char c;
int i;
float f;
clrscr();
printf(“\n enter alphabet, number, float=”);
scanf(“%c%d%f”,&c,&i,&f);
printf(“\n address of (c) %c=%u”,c,&c);
printf(“\n address of (i) %d=%u”,i,&i);
printf(“\n address of(f) %.2f=%u”,f,&f);
getche();
}

WAP to show pointer of any data type that occupies same space.
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
printf(“\t char %d byte & its pointer %d bytes\n”, sizeof(char),sizeof(char*));
printf(“\t int %d byte & its pointer %d bytes\n”, sizeof(int),sizeof(int*));
printf(“\t float %d byte & its pointer %d bytes\n”, sizeof(float),sizeof(float*));
getche();
}

WAP to display the value of variable and its location-using pointer.

#include<stdio.h>
#include<conio.h>
void main()
{
int v=10,*p;
clrscr();
p=&v;
printf(“\n address of v=%u”,p);
printf(“\n value of v=%d”,*p);
printf(“\n address of p=%u”,&p);
getche();
}

WAP to print value of variable using different pointer notations.

#include<stdio.h>
#include<conio.h>
void main()
{
int v=10,*p;
clrscr();
p=&v;
printf(“\n v=%d v=%d”,v,*(&v),*p);
getch();
}
WAP to add two numbers through variables & their pointers.
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c,d,*ap,*bp;
clrscr();
printf(“\n Enter two number :”);
scanf(“%d%d”,&a,&b);
ap=&a;
bp=&b;
c=a+b;
d=*ap+*bp;
printf(“\n sum of a & b using variable :%d”,c);
printf(“\n sum of a & b using pointer :%d”,d);
getche();
}

WAP to assign pointer value to another variable.

#include<stdio.h>
#include<conio.h>
void main()
{
int a=5,b,*c;
c=&a;
b=*c;
clrscr();
printf(“\n memory location of ‘a’=%u”,c);
printf(“the value of a=%d” & b=%d”,a,b);
getche();
}

WAP to assign value of ‘b’ through pointers. Show the effect of addition before and
after assignment of value of ‘b’ to ‘a’.

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,*pa,*pb;
clrscr();
printf(“enter value of ‘a’ & ‘b’ :”);
scanf(“%d%d”,&a,&b);
pa=&a;
pb=&b;
printf(“\n value of a=%d & b=%d”,a,b);
printf(“\n address of a=%u”,pa);
printf(“\n address of b=%u”,pb);
printf(“\n addition of ‘a’ & ‘b’ : %d”,*pa + *pb);
pa=pb;
printf(“\n *pa=%d *pb=%d”,*pa,*pb);
printf(“\n pa=%u pb=%u”,pa,pb);
printf(“\n addition of *pa + *pb : %d”,*pa + *pb);
getche();
}
Arithmetic operations with pointers

Arithmetic operations on pointer variables are also possible. Increase, decrease, prefix &
postfix operations can be performed with the help of the pointers.

Data Type Initial Address Operation Address Required


After Bytes
Operations
int i=2 4046 ++ -- 4048 4044 2
char c=’x’ 4053 ++ -- 4054 4053 1
float f=2.2 4058 ++ -- 4062 4054 4
long l=2 4060 ++ -- 4064 4056 4

WAP to show the effect of increment on pointers variables. Display the memory
locations of integer, character, and floating point numbers before and after
increment of pointers.

#include<stdio.h>
#include<conio.h>
void main()
{
int x,*x1;
char y,*y1;
float z,*z1;
clrscr();
printf(“\n enter integer, character, float values\n :”);
scanf(“%d%c%f”,&x,&y,&z);
x1=&x;
y1=&y;
z1=&z;
printf(“\n address of x=%u”,x1);
printf(“\n address of y=%u”,y1);
printf(“\n address of c=%u”,z1);
x1++;
y1++;
z1++;
printf(“\n After Increment in Pointers”);
printf(“\n Now address of x=%u”,x1);
printf(“\n Now address of y=%u”,y1);
printf(“\n Now address of z=%u”,z1);
printf(“\n Size of Integer :%d”,sizeof(x));
printf(“\n size of character :%d”,sizeof(y));
printf(“\n size of float :%d”,sizeof(z));
getche();
}

WAP to show the effect of increased and decreased operator used as prefix and
suffix with pointer variable.

#include<stdio.h>
#include<conio.h>
void main()
{
int i,*ii;
clrscr();
printf(“\n enter value of i=”);
scanf(“%d”,&i);
ii=&i;
clrscr();
printf(“address of i=%u\n”,ii);
printf(“address of i=%u\n”,++ii);
printf(“address of i=%u\n”,ii++);
printf(“address of i=%u\n”,--ii);
printf(“address of i=%u\n”,ii--);
printf(“address of i=%u\n”,ii);
getche();
}

WAP to perform different arithmetic operations using pointers.

#include<stdio.h>
#include<conio.h>
void main()
{
int a=25,b=10,*p,*j;
p=&a;
j=&b;
clrscr();
printf(“\n addition a+b=%d”,*p+b);
printf(“\n subtraction a-b=%d”,*p-b);
printf(“\n product a*b=%d”,*p**j);
printf(“\n division a/b=%d”,*p/*j);
printf(“\n a mod b=%d”,*p%*j);
getche();
}

Pointers and arrays


Array name by 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. Array elements are always stored in
contiguous memory locations.

#include<stdio.h>
#include<conio.h>
void main()
{
int x[]={2,4,6,8,10},k=1;
clrscr();
while(k<=5)
{
printf(“%3d”,k[x-1]);
k++;
}
}

WAP to find sum of all the elements of an array. Use array name itself as a pointer.

#include<stdio.h>
#include<conio.h>
void main()
{
int sum=0,i=0,a[]={1,2,3,4,5};
clrscr();
printf(“elements values address\n\n”);
while(i<5)
{
printf(“a[%d]\t%5d\t%8u\n”,i,*(a+i),(a+i));
sum=sum+*(a + i++);
}
printf(“\n sum of array elements =%d”,sum);
getch();
}
WAP to display sum of squares and cubes of array elements using pointer.

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int b[]={1,2,3,4,5}, sumsq=0,sumc=0;
clrscr();
for(j=0;j<5;j++)
{
sumsq=sumsq+pow(*(j+b),2);
sumc=sumc+pow(b[j],3);
}
printf(“\n sum of squares of array elements :%d”,sumsq);
printf(“\n sum of cubes of array elements :%d”,sumc);
getch();
}

Pointers and Two-dimensional arrays

A matrix can represent two-dimensional elements of an array.

WAP to display array elements and their address using pointers.

#include<stdio.h>
#include<conio.h>
void main()
{
int i,j=1,*p;
int a[3][3]={{1,2,3},{4,5,6},{7,8,9}};
clrscr();
printf(“\t elements of an array with their addresses\n\n”);
p=&a[0][0];
for(i=0;i<9;i++,j++)
{
printf(“%5d[%5u]”,*(p),p);
p++;
if(j==3)
{
printf(“\n”);
j=0;
}
}
getch();
}

Array of Pointers

WAP to store addresses of different elements of an array-using array of pointers.

#include<stdio.h>
#include<conio.h>
void main()
{
int *arrp[3];
Int arr1[3]={5,10,15},k;
clrscr();
for(k=0;k<3;k++)
{
arrp[k]=arr1+k;
printf(“\n\t address element\n”);
for(k=0;k<3;k++)
{
printf(“\t%u”,arrp[k]);
printf(“”\t%7d\n”,*(arrp[k]);
}
getch();
}
Pointers to pointers

Pointer is known as a variable containing address of another variable. The pointer


variables also have an address. The pointer variable containing address of another
variables is called as pointer to pointer. This chain can be continued to any extent.

WAP to print value of a variable though pointer and pointer to pointer.

#include<stdio.h>
#include<conio.h>
void main()
{
int a=2,*p,**q;
p=&a;
q=&p;
clrscr();
printf(\n value of a=%d address of a =%u”,a,&a);
printf(“\n through *p value of a=%d address of a=%u”,*p,p);
printf(“\n through **q value of a=%d address of a=%d”,**q,*q);
getche();
}

Pointers and strings

WAP to read string from keyboard and display it using character pointer.

#include<stdio.h>
#include<conio.h>
void main()
{
char name[15],*ch;
clrscr();
printf(“\n enter your name :”);
gets(name);
ch=name;
while(*ch!=’\0’)
{
printf(“%c”,*ch);
ch++;
}
getche();
}
WAP to find length of a given string including and excluding spaces using pointers.

#include<stdio.h>
#include<conio.h>
void main()
{
char str[20],*s;
int p=0,q=0;
clrscr();
printf(“\n enter string :”);
gets(str);
s=str;
while(*s!=’\0’)
{
printf(“%c”,*s);
p++;
s++;
if(*s==32)
q++;
}
printf(“\n length of string including spaces :%d”,p);
printf(“\n length of string excluding paces :%d”,p-q);
getche();
}
WAP to read two strings through the keyboard. Compare these two strings
character by character. Display the similar characters found in both the strings and
count the number of dissimilar characters.

#include<stdio.h>
#include<conio.h>
void main()
{
char str1[20],str2[20],l,*a,*b;
int c=0;
clrscr();
printf(“\n enter first string :”);
gets(str1);
printf(“\n enter second string :”);
gets(str2);
a=str1;
b=str2;
printf(“\n similar charaters found in both string :”);
while(*a!=’\0’)
{
if(stricmp(*a,*b)==0)
{
printf(“\n\t%c\t%c”,*a,*b);
l++;
}
else
c++;
a++;
b++;
}
if(c==0)
printf(“\n the string are identical “);
else
printf(“\n the strings are different at %d places”,c);
printf(“\n the string charaters are similar at %d paces.”,l);
getche();
}

You might also like