Module 4.2 - Pointers
Module 4.2 - Pointers
INTRODUCTION
Every variable in C has a name and a value associated with it. When a variable is declared, a
specific block of memory within the computer is allocated to hold the value of that variable. The
size of the allocated block depends on the type of the data.
int x = 10;
When this statement executes, the compiler sets aside 2 bytes of memory to hold the value 10. It
also sets up a symbol table in which it adds the symbol x and the relative address in memory
where those 2 bytes were set aside.
Thus, every variable in C has a value and an also a memory location (commonly known as
address) associated with it.
We can "dereference" a pointer, i.e. refer to the value of the variable to which it points by using
unary '*' operator as in *ptr. That is, *ptr = 10, since 10 is value of x.
#include<stdio.h>
int main()
{
int num, *pnum;
pnum = #
printf(“\n Enter the number : “);
scanf(“%d”, &num);
printf(“\n The number that was entered is : %d”, *pnum);
printf(“\n The number that was entered is : %p”, &num);
return 0;
}
OUTPUT:
Enter the number : 10
The number that was entered is : 10
The number that was entered is : (address of num)
We can also assign values to variables using pointer variables and modify their values.
#include<stdio.h>
int main()
{
int num, *pnum;
pnum = #
*pnum=10;
printf(“*pnum= %d”, *pnum);
printf(“num= %d”, num);
*pnum=*pnum+1;
printf(“\n After increment *pnum = %d”, *pnum);
printf(“\n After increment num = %d”, num);
return 0;
}
OUTPUT:
*pnum=10
num=10
After increment *pnum =11
After increment num =11
TYPES OF POINTERS
1. NULL POINTERS
A null pointer, which is a special pointer that does not point to any value. This means that
a NULL pointer does not point to any valid memory address. To declare a null pointer
you may use the predefined constant NULL,
int *ptr = NULL;
You can always check whether a given pointer variable stores address of some variable or
contains a null by writing,
if ( ptr == NULL)
{
Statement block;
}
You may also initialize a pointer as an null pointer by using a constnt 0, as shown below.
int ptr;
ptr=0;
Null pointers are used in situations if one of the pointers in the program points somewhere
some of the time but not all of the time. In such situations it is always better to set it to a
null pointer when it doesn't point anywhere valid, and to test to see if it's a null pointer
before using it.
2. GENERIC POINTERS
A generic pointer is pointer variable that has void as its data type.
The generic pointer, can be pointed at variables of any data type.
It is declared by writing
void *ptr;
You need to cast a void pointer to another kind of pointer before using it. Generic pointers
are used when a pointer has to point to data of different types at different times.
For ex,
#include
int 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);
return 0;
}
OUTPUT:
Generic pointer points to the integer value = 10
Generic pointer now points to the character = A
The calling function sends the addresses of the variables and the called function must declare
those incoming arguments as pointers. In order to modify the variables sent by the caller, the
called function must dereference the pointers that were passed to it. Thus, passing pointers to a
function avoid the overhead of copying data from one function to another.
Write a program to add two numbers using function.
#include
int main()
{
int num1, num2, total;
printf(“\n Enter two numbers : “);
scanf(“%d %d”, &num1, &num2);
sum(&num1, &num2, &total);
printf(“\n Total = %d”, total);
return 0;
}
void sum ( int *a, int *b, int *t)
{
*t = *a + *b;
}
OUTPUT
Enter two numbers: 2 3
Total = 5
Swapping two numbers
#include<stdio.h>
void swap(int *p,int *q);
int main()
{
int a,b;
printf("Enter the value of a\n");
scanf("%d",&a);
printf("Enter the value of b\n");
scanf("%d",&b);
printf("Before swapping\n");
printf("a = %d b = %d\n",a,b);
swap(&a,&b);
printf("After swapping\n");
printf("a = %d b = %d\n",a,b);
return 0;
}
void swap(int *p,int *q)
{
int temp;
temp = *p;
*p = *q;
*q = temp;
}
Develop a program using pointers to compute the sum, mean and standard deviation
of all elements stored in an array of N real numbers.
#include<stdio.h>
#include<math.h>
int main()
{
float a[10];
int n,i;
float mean,dev,sum=0,sum2=0,var;
float *ptr=a;
printf("Enter the number of elements in the array\n");
scanf("%d",&n);
printf("Enter the elements in the array\n");
for(i=0;i<n;i++)
{
scanf("%f",&a[i]);
}
for(i=0;i<n;i++)
{
sum=sum+*ptr;
ptr++;
}
mean = sum/n;
ptr=0;
for(i=0;i<n;i++)
{
num = num + pow((*ptr - mean), 2);
}
var = num/n;
dev = sqrt(var);
printf("Sum is %.2f\nMean is %.2f\nStandard deviation is %.2f",sum,mean,dev);
return 0;