UNIT 3
UNIT 3
Pointers
Introduction to Pointer
The pointer in C language is a variable which stores the address of another variable.
This variable can be of type int, char, array, function, or any other pointer.
The size of the pointer depends on the architecture.
However, in 32-bit architecture the size of a pointer is 2 byte.
Consider the following example to define a pointer which stores the address of an integer.
int n = 10;
int* p = &n; // Variable p of type pointer is pointing to the address of the variable n of ty
pe integer.
Declaring a pointer
The pointer in c language can be declared using * (asterisk symbol). It is also known as
indirection pointer used to dereference a pointer.
int *a;//pointer to int
char *c;//pointer to char
Pointer Example
As you can see in the above figure, pointer variable stores the address of number
variable, i.e., fff4. The value of number variable is 50. But the address of pointer variable
p is aaa3.
By the help of * (indirection operator), we can print the value of pointer variable p.
#include<stdio.h>
int main(){
int number=50;
int *p;
p=&number;//stores the address of number variable
printf("Address of p variable is %x \n",p);
printf("Value of p variable is %d \n",*p);
return 0;
}
Output
1
Address of p variable is fff4
Value of p variable is 50
Pointer to array
int arr[10];
int *p[10]=&arr; // Variable p of type pointer is pointing to the address of an integer arr
ay arr.
Pointer to a function
void show (int);
void(*p)(int) = &display; // Pointer p is pointing to the address of a function
Pointer to structure
struct st
{
int i;
float f;
}ref;
struct st *p = &ref;
Advantage of pointer
1) Pointer reduces the code and improves the performance, it is used to retrieving
strings, trees, etc. and used with arrays, structures, and functions.
2) We can return multiple values from a function using the pointer.
3) It makes you able to access any memory location in the computer's memory.
Usage of pointer
Dynamic memory allocation
In c language, we can dynamically allocate memory using malloc() and calloc()
functions where the pointer is used.
Arrays, Functions, and Structures
Pointers in c language are widely used in arrays, functions, and structures. It reduces
the code and improves the performance.
In the c programming language, we use normal variables to store user data values.
When we declare a variable, the compiler allocates required memory with the specified
name.
In the c programming language, every variable has a name, data type, value, storage class,
and address.
We use a special type of variable called a pointer to store the address of another variable
with the same datatype.
A pointer is defined as follows...
2
Pointer is a special type of variable used to store the memory location address of a
variable.
In the c programming language, we can create pointer variables of any datatype.
Every pointer stores the address the variable with same datatype only.
That means integer pointer is used store the address of integer variable only.
3
Accessing Variable Value Using Pointer
Pointer variables are used to store the address of other variables.
We can use this address to access the value of the variable through its pointer.
We use the symbol "*" in front of pointer variable name to access the value of variable to
which the pointer is pointing.
We use the following general syntax...
*pointerVariableName
Example Code
#include<stdio.h>
#include<conio.h>
void main()
{
int a = 10, *ptr ;
clrscr();
ptr = &a ;
printf("Address of variable a = %u\n", ptr) ;
printf("Value of variable a = %d\n", *ptr) ;
printf("Address of variable ptr = %u\n", &ptr) ;
}
Pointer variable always points to variables of the same datatype. For example:
float a;
4
int *ptr = &a; // ERROR, type mismatch
While declaring a pointer variable, if it is not assigned to anything then it contains garbage
value. Therefore, it is recommended to assign a NULL value to it,
5
//prints the address of 'a'
printf("%u\n", &a);
printf("%u\n", p);
printf("%u\n", &p); //prints address of 'p'
return 0;
}
output
10
10
3795480300
3795480300
3795480304
6
Relation between Arrays and Pointers
From the above example, it is clear that &x[0] is equivalent to x. And, x[0] is equivalent
to *x.
Similarly,&x[1] is equivalent to x+1 and x[1] is equivalent to *(x+1).
&x[2] is equivalent to x+2 and x[2] is equivalent to *(x+2)....
Basically, &x[i] is equivalent to x+i and x[i] is equivalent to *(x+i).
Example 1: Pointers and Arrays
#include <stdio.h>
int main()
{
int i, x[6], sum = 0;
printf("Enter 6 numbers: ");
for(i = 0; i < 6; ++i)
{
// Equivalent to scanf("%d", &x[i]);
scanf("%d", x+i);
// Equivalent to sum += x[i]
sum += *(x+i);
}
printf("Sum = %d", sum);
return 0;
}
When you run the program, the output will be:
Enter 6 numbers: 2
3
4
4
12
4
Sum = 29
7
The first pointer is used to store the address of a variable whereas the second pointer is
used to store the address of the first pointer.
Let's understand it by the diagram given below.
void pointers
Till now, we have studied that the address assigned to a pointer should be of the same type
as specified in the pointer declaration.
For example, if we declare the int pointer, then this int pointer cannot point to the float
variable or some other type of variable, i.e., it can point to only int type variable.
To overcome this problem, we use a pointer to void.
A pointer to void means a generic pointer that can point to any data type.
We can assign the address of any data type to the void pointer, and a void pointer can be
assigned to any type of the pointer without performing any explicit typecasting.
Syntax of void pointer
8
void *pointer name;
Declaration of the void pointer is given below:
void *ptr;
In the above declaration, the void is the type of the pointer, and 'ptr' is the name of the
pointer.
Size of the void pointer in C
The size of the void pointer in C is the same as the size of the pointer of character type.
According to C perception, the representation of a pointer to void is the same as the
pointer of character type.
The size of the pointer will vary depending on the platform that you are using.
Let's look at the below example:
#include <stdio.h>
int main()
{
void *ptr = NULL; //void pointer
printf("size of void pointer = %d\n\n",sizeof(ptr));
return 0;
}
Advantages of void pointer
The malloc() and calloc() function return the void pointer, so these functions can be
used to allocate the memory of any data type.
The void pointer in C can also be used to implement the generic functions in C.
9
return 0;
}
Pointer to functions
It is possible to declare a pointer pointing to a function which can then be used as an
argument in another function. A pointer to a function is declared as follows,
type (*pointer-name)(parameter);
Here is an example :
int (*sum)(); //legal declaration of pointer to function
int *sum(); //This is not a declaration of pointer to function
A function pointer can point to a specific function when it is assigned the name of that
function.
int sum(int, int);
int (*s)(int, int);
s = sum;
Here s is a pointer to a function sum. Now sum can be called using function pointer s along
with providing the required argument values.
s (10, 20);
Example of Pointer to Function
#include <stdio.h>
int sum(int x, int y)
{
return x+y;
}
int main( )
{
int (*fp)(int, int);
fp = sum;
int s = fp(10, 15);
printf("Sum is %d", s);
return 0;
}
10