Pointers Inc
Pointers Inc
Void Pointer
▪The Void pointers in C are the pointers of type void.
▪It means that they do not have any associated data type.
▪They are also called generic pointers as they can point to any type and can be
typecasted to any type.
▪One of the main properties of void pointers is that they cannot be dereferenced.
Syntax:
void * pointer_name;
Example Program
#include <stdio.h>
int main()
{
int a = 10;
char b = 'x';
// void pointer holds address of int 'a'
void* p = &a;
printf ("Address of a stored in void pointer p =%p\n",p);
// void pointer holds address of char 'b'
p = &b;
printf ("Address of b stored in void pointer p =%p",p);
}
OUTPUT:
Address of a stored in void pointer p =00000031163ffb74
Address of b stored in void pointer p =00000031163ffb73
▪Void pointers cannot be dereferenced #include <stdio.h>
therefore the following program int main()
doesn’t compile and gives an error.
{
▪malloc() and calloc() return void * int a = 10;
type and are used to allocate memory
of any data type. void* ptr = &a;
int main()
{
int a = 10;
void* ptr = &a;
// The void pointer is cast to an integer pointer using '(int*)ptr'
// Then, the value is dereferenced with `*(int*)ptr` to get the
// value at that memory location
printf("%d", *(int*)ptr);
return 0;
OUTPUT:
} 10
▪ The C standard doesn’t allow pointer arithmetic with void pointers. However, in
GNU C it is allowed by considering the size of the void as 1.
▪EXAMPLE PROGRAM
#include <stdio.h>
int main()
{
int a[2] = { 8, 6 };
// Declare a void pointer and assign the address of
// array 'a' to it
void* ptr = &a;
// Increment the pointer by the size of an integer
ptr = ptr + sizeof(int);
// The void pointer 'ptr' is typecasted to an integer
// pointer Then, the value is dereferenced get the value at
// that memory location
printf("%d", *(int*)ptr);
return 0;
}
OUTPUT: Note : The above program may not work in other compilers.
6
Pointers v/s Array
S.No. Array Pointer
1. It stores the values of a It stores the address of variables.
homogeneous data type.
2. An array is defined as a collection Pointer is a variable which stores
of similar datatypes. address of another variable.
3. The number of variables that can be Pointer can store the address of
stored is decided by the size of the only a single variable.
array.
4. The initialization of arrays can be Pointers cannot be initialized
done while defining them. while defining them.
S.No. Array Pointer
5. The nature of arrays is static. The nature of pointers is dynamic.
Arrays cannot be resized according Pointers can be resized at any
6.
to user's requirements. point of time.
The allocation of array is done at The allocation of pointer is done
7.
compile time. at run time.
Call by Value
▪we have always passed the ‘values’ of variables to the called function which is
know as call by value.
▪In call by value method of parameter passing, the values of actual parameters are
copied to the function’s formal parameters.
▪There are two copies of parameters stored in different memory locations.
▪One is the original copy and the other is the function copy.
▪Any changes made inside functions are not reflected in the actual parameters .
Program to illustrate call by value
#include <stdio.h>
// Function Prototype
void swapx(int x, int y);
int main()
{
int a = 10, b = 20;
// Pass by Values
swapx(a, b); // Actual Parameters
printf("Actual Parameters:\na = %d b = %d\n", a, b);
return 0;
}
// Swap functions that swaps
// two values
void swapx(int x, int y) // Formal Parameters
{
int t;
t = x;
x = y;
y = t;
// Function Prototype
void swapx(int*, int*);
// Main function
int main()
{
int a = 10, b = 20;
// Pass reference
swapx(&a, &b); // Actual Parameters
printf("Actual Parameters of the caller:\na = %d b = %d\n", a, b);
return 0;
}
t = *x;
*x = *y;
*y = t;
ptr =
400 bytes of Memory
int main()
{
return 0;
}
OUTPUT:
ENTER THE ELEMENTS IN THE MEMORY
34
45
36
23
87
The elements are: 34 45 36 23 87
calloc()
▪"calloc" stands for contiguous allocation.
▪The malloc() function allocates memory and leaves the memory uninitialized,
whereas the calloc() function allocates memory and initializes all bits to zero.
▪Syntax:
ptr = (castType*)calloc(n, size);
▪Example:
ptr = (float*) calloc(25, sizeof(float));
The above statement allocates contiguous space in memory for 25 elements of
type float.
free()
▪Dynamically allocated memory created with either calloc() or malloc() doesn't
get freed on their own. You must explicitly use free() to release the space.
▪Syntax:
free(ptr);
Where ptr is the pointer to the memory block that needs to be freed or
deallocated.
▪The free() function cannot be used to free the statically allocated memory (e.g.,
local variables) or memory allocated on the stack.
Program to illustrate malloc() and free()
// Program to calculate the sum of n numbers entered by the user
#include <stdio.h>
#include <stdlib.h>
int main() {
int n, i, *ptr, sum = 0;
*ptr = 10;
*(ptr + 1) = 20;
printf("elements of ptr before reallocation\n");
for(i = 0; i < 2; i++)
printf("%d ", *(ptr + i));
ptr_new = (int *)realloc(ptr, sizeof(int)*3);
*(ptr_new + 2) = 30;
return 0;
}
OUTPUT:
elements of ptr before reallocation
10 20
elements of ptr_new after reallocation
10 20 30
Pointer to an Array
#include<stdio.h> ▪In this program, A pointer ptr points to
the 0th element of the array.
int main()
▪Similarly, a pointer can be declared that
{ can point to whole array instead of only
int arr[5] = { 1, 2, 3, 4, 5 }; one element of the array.
int *ptr = arr; ▪Pointer to an array is useful when
talking about multidimensional arrays.
printf("%p\n", ptr);
return 0;
}
▪Syntax:
data_type (*var_name)[size_of_array];
▪Example:
int (*ptr)[10];
▪Here ptr is pointer that can point to an array of 10 integers.
▪Since subscript have higher precedence than indirection, it is necessary to
enclose the indirection operator and pointer name inside parentheses.
Program to illustrate difference between pointer
to an integer and pointer to an array of integers.
#include<stdio.h>
int main()
{
// Pointer to an integer
int *p;
p++;
ptr++;
return 0;
}
OUTPUT:
p = 000000bb7bdffbf0, ptr = 000000bb7bdffbf0
p = 000000bb7bdffbf4, ptr = 000000bb7bdffc04
▪ The base type of p is int while base type of ptr is ‘an array of 5 integers’.
// creating an array
int arr[] = { 1, 2, 3 };
int i, *ptr[SIZE];
// assigning the address of integer.
for (i = 0; i < SIZE; i++)
ptr[i] = &arr[i];
}
OUTPUT:
Value of arr[0] = 1
Value of arr[1] = 2
Value of arr[2] = 3
Program to demonstrate array of pointers
to store a list of strings.
#include <stdio.h>
int main()
{
// Assigning function address using & operator
int (*add_ptr)(int, int) = &add;
return 0;
}
▪An array of function pointers is possible, just like with regular pointers.
▪Function pointer can be used in place of switch case.
▪A function pointer can be passed as an argument and can also be returned from a
function.
Program to illustrate an array of function
pointers
#include <stdio.h>
void add(int a, int b)
{
printf("Addition is %d\n", a+b);
}
void subtract(int a, int b)
{
printf("Subtraction is %d\n", a-b);
}
void multiply(int a, int b)
{
printf("Multiplication is %d\n", a*b); }
int main()
{
// fun_ptr_arr is an array of function pointers
void (*fun_ptr_arr[])(int, int) = {add, subtract, multiply};
unsigned int ch, a = 15, b = 10;
(*fun_ptr_arr[ch])(a, b);
return 0;
}
OUTPUT:
Enter Choice: 0 for add, 1 for subtract and 2 for multiply
2
Multiplication is 150
Program to illustrate function pointers as
parameter
#include <stdio.h>
Number 1: 34
Number 2: 45
Number 3: 36
Number 4: 28
Number 5: 89
The Largest element is : 89.00
Program to sort an array using pointer
#include <stdio.h>
void main()
{
int *a,i,j,tmp,n;