0% found this document useful (0 votes)
7 views9 pages

Unit 6 - Pointers

Pointers are variables that store memory addresses of other variables of the same data type, utilizing operators like *, &, and -> for various operations. They enhance program efficiency, enable dynamic memory management, and facilitate complex data structures. Pointer arithmetic allows manipulation of memory addresses, and pointers can be used in functions to pass arguments by reference.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views9 pages

Unit 6 - Pointers

Pointers are variables that store memory addresses of other variables of the same data type, utilizing operators like *, &, and -> for various operations. They enhance program efficiency, enable dynamic memory management, and facilitate complex data structures. Pointer arithmetic allows manipulation of memory addresses, and pointers can be used in functions to pass arguments by reference.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

POINTERS

DEFINITION:
A pointer is a variable that stores memory address of another variable which is of similar data
type.

The Pointer Operators

Use of * , & and –> Operator:

1. * (asterisk) “Indirection operator (Pointer operator)” can be used to get the value stored at
the address pointed by any pointer variable. It can also be used in pointer declaration just to
indicate the type of the variable is a pointer type.
2. & (ampersand) “Address of operator” can be used to determine address of any variable.
3. The –>”Arrow operator”, consists of the minus sign followed by a greater than sign.
The arrow is used in place of the dot operator when you are accessing a structure
member through a pointer to the structure.

Pointer Declaration:
datatype *pointer_variable;
e.g.
int *ptr; //Pointer declaration

Pointer Initialization:
datatype *pointer_variable;
pointer_variable =&name_of_variable;
e.g.

int x=10; //variable declaration x is assigned value 10

int *ptr; //Pointer declaration

ptr=&x; //ptr variable is initialized to the address of variable x;

Sample Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int x=10;
int *ptr; //Pointer declaration
clrscr();
ptr=&x; // address of x is stored in ptr
printf("Value of x=%d",x);
printf("\nAddress of x=%u",ptr);
printf("\nValue of *ptr=%d",*ptr);
getch();
}
Output:
Value of x=10
Address of x=65524
Value of *ptr=10

ADVANTAGES OF POINTER:
1. Pointers reduce the length and complexity of a program.
2. They increase execution speed.
3. A pointer enables us to access a variable that is defined outside the function.
4. Pointers are more efficient in handling the data tables.
5. The use of a pointer array of character strings results in saving of data storage space in
memory.
6. It supports dynamic memory management.
7. Pointers can be used to pass information back and forth between the calling function
and called function.
8. Pointers helps us to build complex data structures like linked list, stack, queues, trees,
graphs etc.

POINTER ARITHMETIC
The pointer arithmetic is done as per the data type of the pointer.
The basic operations on pointers are:

1. Increment:
It is used to increment the pointer. Each time a pointer is incremented, it points to the next
location with respect to memory size .
Example,
If ptr is an integer pointer stored at address 1000,then ptr++ shows 1002 as incremented
location for an int. It increments by two locations as it requires two bytes storage.

2. Decrement:
It is used to decrement the pointer. Each time a pointer is decremented, it points to the
previous location with respect to memory size.
Example,
If the current position of pointer is 1002, then decrement operation ptr-- results in the
pointer pointing to the location 1000 in case of integer pointer as it require two bytes storage.

3. Addition
When addition operation is performed on pointer, it gives the location incremented by the
added value according to data type.
Eg:
If ptr is an integer pointer stored at address 1000, Then ptr+2 shows 1000+(2*2) = 1004 as
incremented location for an int.

4. Subtraction
When subtraction operation is performed on the pointer variable, it gives the location
decremented by the subtracted value according to data type.
Eg:
If ptr is an integer pointer stored at address 1004, Then ptr-2 shows 1004-(2*2) = 1000
as decremented location for an int.

Sample Program on Pointer Arithmetic:


#include<stdio.h>
#include<conio.h>
void main()
{
int i = 10;
int *ptr=&i;
clrscr();
printf("%x%d",ptr,i);
ptr++;
printf("\n%x%d",ptr,i);
printf("\n%x",ptr+2);
printf("\n%x",ptr-2);
getch();
}

HANDLING ARRAYS USING POINTERS:


main( )
{
int num[ ] = { 24, 34, 12, 44, 56, 17 } ;
int i, *j ;
j = &num[0] ; /* assign address of zeroth element */
for ( i = 0 ; i <= 5 ; i++ )
{
printf ( "\naddress = %u ", j ) ;
printf ( "element = %d", *j ) ;
j++ ; /* increment pointer to point to next location */
}
}

The output of this program would be:


address = 65512 element = 24
address = 65514 element = 34
address = 65516 element = 12
address = 65518 element = 44
address = 65520 element = 56
address = 65522 element = 17

In this program, to begin with we have collected the base address of the array (address of
the 0th element) in the variable j using the statement,

j = &num[0] ; /* assigns address 65512 to j */

When we are inside the loop for the first time, j contains the address 65512, and the
value at this address is 24. These are printed using the statements,

printf ( "\naddress = %u ", j ) ;


printf ( "element = %d", *j ) ;

On incrementing j it points to the next memory location of its type (that is location no. 65514).
But location no. 65514 contains the second element of the array, therefore when the
printf( ) statements are executed for the second time they print out the second element of
the array and its address (i.e. 34 and 65514)... and so on till the last element of the array has
been printed.

HANDLING FUNCTIONS USING POINTERS


When pointer (addresses) is passed to the function as an argument instead of value then
function is called as call by reference.
Example:
#include<stdio.h>
#include<conio.h>
int add(int *);
void main()
{
int *ptr,pos=0;
clrscr();
printf("Enter position:");
scanf("%d",&pos);
ptr=&pos;
printf("\nSum=%d",add(ptr));
getch();
}
int add(int *p)
{
int i=0;
int sum=0;
for(i=1;i<=(*p);i++)
{
sum=sum+i;
}
return sum;
}
In the above program function passes the address of „pos‟ to the ptr. The value of ptr is passed
while calling the function. In function definition in *p it takes value of ptr instead of address for
performing addition of numbers up to specific position.
PROGRAMS ASKED IN EXAM ON POINTERS

1. Write a program to Print values of variables and their addresses.

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
clrscr();
a=5;
b=10;
printf("\n Value of a=%d",a);
printf("\n Address of a=%u",&a);
printf("\n Value of b=%d",b);
printf("\n Address of b=%u",&b);
getch();
}

2. Write a program to print reverse of a entered string using pointer

#include<stdio.h>
#include<conio.h>
void main()
{
char str[10],*ptr;
int i=0;
clrscr();
printf("Enter string:");
scanf("%s",str);
ptr=str;
while(*ptr!='\0')
{
i=i+1;
ptr=ptr+1;
}
while(i>0)
{
ptr=ptr-1;
printf("%c",*ptr);
i=i-1;
}
getch();
}
3. Write a program to accept two numbers from user and perform addition, subtraction,
multiplication and division operations using pointer.
#include<stdio.h>
#include<conio.h>
void main()
{
int no1,no2,*ptr1,*ptr2,result;
clrscr();
printf("Enter no1:");
scanf("%d",&no1);
printf("\nEnter no2:");
scanf("%d",&no2);
ptr1=&no1;
ptr2=&no2;
result=*ptr1 + *ptr2;
printf("\n Addition=%d",result);
result=*ptr1 - *ptr2;
printf("\n Subtraction=%d",result);
result=*ptr1 * *ptr2;
printf("\n Multiplication=%d",result);
result=*ptr1 / (*ptr2);
printf("\n Division=%d",result);
getch();
}
4. Write a program to compute the sum of all elements stored in an array using pointers.
#include<stdio.h>
#include<conio.h>
void main()
{
int a[5],sum=0,i,*ptr;
clrscr();
printf("\n Enter array elements:");
for(i=0;i<5;i++)
scanf("%d",&a[i]);
ptr=&a[0]; //pointer points to base of an array
for(i=0;i<5;i++)
{
sum=sum+(*ptr);
ptr=ptr+1;
}
printf("\n Sum= %d",sum);
getch();
}
5. Write a program to exchange values of two variables using pointer.
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,*p1,*p2,temp;
clrscr();
printf("enter value of a");
scanf("%d",&a);
printf("enter value of b");
scanf("%d",&b);
printf("\nvalues before exchange are a=%d b=%d",a,b);
p1=&a;
p2=&b;
temp=*p1;
*p1=*p2;
*p2=temp;
printf("\nvalues after exchange are a=%d b=%d",a,b);
getch();
}

You might also like