Module4 Chapter2
Module4 Chapter2
Pointers
4.6 Introduction
“A pointer is a variable that holds the address of another variable”. or
A pointer is a variable that contains the memory location of another variable. Therefore, a
pointer is a variable that represents the location of a data item, such as a variable or an array
element.
Applications of pointer
Pointers are used to pass information back and forth between functions.
Pointers enable the programmers to return multiple data items from a function via function arguments.
Pointers provide an alternate way to access the individual elements of an array.
Pointers are used to pass arrays and strings as function arguments.
Pointers are used to create complex data structures, such as trees, linked lists, linked stacks, linked
queues, and graphs.
Pointers are used for the dynamic memory allocation of a variable.
Example:
1. int *ptr; // declares a pointer variable ptr of integer type.
2. float *temp; // declares a pointer variable temp of floating type.
Example:
int a=3;
int *ptr;
ptr=&a;
Dr. Azizkhan F Pathan, Prof. Meghana G R, Prof. Kotramma T S, Dept. of CS&E, JIT, Davangere 1
ptr a
Memory layout:
65530 3
Address: 65530
‘ptr = &a’ copies the address of ‘a’ to the pointer variable ‘ptr’.
Example Program: Write a C program to print value and address of the variable using pointers.
#include<stdio.h> Output:
void main () The address of a=65530 and value of a=20
{
int a=20, *ptr;
ptr = &a; //ptr1 is a pointer to variable a
printf(“The address of a=%d and value of a=%d\n”,ptr,*ptr);
}
ptr1 a
Memory layout:
65530 20
Address: 65530
Syntax
data_type * ptr_name = address_of_variable;
where,
data_type:.It can be int, float, char etc.
Asterisk (*): It tells the compiler that we are declaring a pointer variable.
ptr_name: It is the name of the pointer variable.
address_of_variable: Itis the address of another variable.
Example:
int a;
int *ptr;
ptr=&a;
or
int a;
int *ptr=&a;
Both are equivalent.
We can dereference a pointer, i.e., refer to the value of the variable to which it points, by using unary
'*' operator (also known as indirection operator) as *pnum, i.e., *pnum = 10, since 10 is value of x.
Therefore, * is equivalent to writing value at address. Look at the code below which shows the use of
pointer variable.
#include <stdio.h>
void main()
{
int num, *pnum;
pnum = #
Dr. Azizkhan F Pathan, Prof. Meghana G R, Prof. Kotramma T S, Dept. of CS&E, JIT, Davangere 2
printf(“Enter the number : ");
scanf("%d", &num);
printf("\n The number that was entered is : %d", *pnum);
}
Output:
Enter the number : 10
The number that was entered is : 10
What will be the value of *(&num)? It is equivalent to simply writing num.
Dr. Azizkhan F Pathan, Prof. Meghana G R, Prof. Kotramma T S, Dept. of CS&E, JIT, Davangere 3
#include <stdio.h>
void main()
{
int x=10;
char ch = ‘A’;
void *gp;
gp = &x;
printf("\n Generic pointer points to the integer value = %d", *(int*)gp);
gp = &ch;
printf("\n Generic pointer now points to the character= %c", *(char*)gp);
}
Output:
Generic pointer points to the integer value = 10
Generic pointer now points to the character = A
void main()
{
Output:
int a,b, res;
printf(“Enter the values of a and b:”); Enter the values of a and b: 4 5
scanf(“%d%d”,&a,&b); result =9
res = add(&a,&b);
printf(“result =%d\n”, res);
}
Dr. Azizkhan F Pathan, Prof. Meghana G R, Prof. Kotramma T S, Dept. of CS&E, JIT, Davangere 4
2. Write a C program to swap two numbers using call by reference.
#include<stdio.h>
void swap(int *a,int *b)
{
int temp;
temp=*a;
*a=*b;
*b=temp;
}
void main()
{
int a,b;
printf(“Enter the values of a and b:”); Enter the values of a and b: 10 20
scanf(“%d%d”,&a,&b); Before swapping: a=10 b=20
printf(“Before swapping: a=%d\tb=%d”, a, b); After swapping: a=20 b=10
swap(&a,&b);
printf(“After swapping: a=%d\tb=%d”, a, b)
}
Output:
Enter the values of a and b: 10 20
Before swapping: a=10 b=20
After swapping: a=20 b=10
Dr. Azizkhan F Pathan, Prof. Meghana G R, Prof. Kotramma T S, Dept. of CS&E, JIT, Davangere 5
inprotected.com