new UNIT-IV

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 46

UNIT-IV FUNCTIONS AND POINTERS

Modular programming - Function prototype, function definition, function call,


Built-in functions (string functions, math functions) – Recursion, Binary Search
using recursive functions – Pointers – Pointer operators – Pointer arithmetic –
Arrays and pointers – Array of pointers – Parameter passing: Pass by value, Pass
by reference.

Functions
In c, we can divide a large program into the basic building blocks known as function. The
function contains the set of programming statements enclosed by {}. A function can be called
multiple times to provide reusability and modularity to the C program. In other words, we can
say that the collection of functions creates a program. The function is also known as procedure
or subroutine in other programming languages.

Advantage of functions in C
● There are the following advantages of C functions.
● By using functions, we can avoid rewriting same logic/code again and again in a
program.
● We can call C functions any number of times in a program and from any place in a
program.
● We can track a large C program easily when it is divided into multiple functions.
● Reusability is the main achievement of C functions.
● However, Function calling is always a overhead in a C program.

Function Aspects
There are three aspects of a C function.
Function declaration A function must be declared globally in a c program to tell the compiler
about the function name, function parameters, and return type.

Function call Function can be called from anywhere in the program. The parameter list must not
differ in function calling and function declaration. We must pass the same number of functions
as it is declared in the function declaration.

Function definition It contains the actual statements which are to be executed. It is the most
important aspect to which the control comes when the function is called. Here, we must notice
that only one value can be returned from the function.
SN C function aspects Syntax

1 Function declaration return_type function_name (argument list);

2 Function call function_name (argument_list)

3 Function definition return_type function_name (argument list) {function body;}

The syntax of creating function in c language is given below:


Pau
return_type function_name(data_type parameter...)
{
//code to be executed
}

Types of Functions
There are two types of functions in C programming:
Library Functions: are the functions which are declared in the C header files such as scanf(),
printf(), gets(), puts(), ceil(), floor() etc.
User-defined functions: are the functions which are created by the C programmer, so that he/she
can use it many times. It reduces the complexity of a big program and optimizes the code.

Return Value
A C function may or may not return a value from the function. If you don't have to return any
value from the function, use void for the return type.
Let's see a simple example of C function that doesn't return any value from the function.
Example without return value:
void hello(){
printf("hello c");
}
If you want to return any value from the function, you need to use any data type such as int, long,
char, etc. The return type depends on the value to be returned from the function.
Let's see a simple example of C function that returns int value from the function.

Example with return value:


int get(){
return 10;
}
In the above example, we have to return 10 as a value, so the return type is int. If you want to
return floating-point value (e.g., 10.2, 3.1, 54.5, etc), you need to use float as the return type of
the method.

float get(){
return 10.2;
}
Now, you need to call the function, to get the value of the function.

Different aspects of function calling or Function Prototypes


A function may or may not accept any argument. It may or may not return any value.
Based on these facts, there are four different aspects of function calls.
● function without arguments and without return value
● function without arguments and with return value
● function with arguments and without return value
● function with arguments and with return value

1. Example for Function without argument and return value


Example 1
#include<stdio.h>
void printName();
void main ()
{
printf("Hello ");
printName();
}
void printName()
{
printf("Javatpoint");
}
Output
Hello Javatpoint

Example 2
#include<stdio.h>
void sum();
void main()
{
printf("\nGoing to calculate the sum of two numbers:");
sum();
}
void sum()
{
int a,b;
printf("\nEnter two numbers");
scanf("%d %d",&a,&b);
printf("The sum is %d",a+b);
}
Output
Going to calculate the sum of two numbers:
Enter two numbers 10
24
The sum is 34

2. Example for Function without argument and with return value


Example 1
#include<stdio.h>
int sum();
void main()
{
int result;
printf("\nGoing to calculate the sum of two numbers:");
result = sum();
printf("%d",result);
}
int sum()
{
int a,b;
printf("\nEnter two numbers");
scanf("%d %d",&a,&b);
return a+b;
}
Output
Going to calculate the sum of two numbers:

Enter two numbers 10


24

The sum is 34
Example 2: program to calculate the area of the square
#include<stdio.h>
int sum();
void main()
{
printf("Going to calculate the area of the square\n");
float area = square();
printf("The area of the square: %f\n",area);
}
int square()
{
float side;
printf("Enter the length of the side in meters: ");
scanf("%f",&side);
return side * side;
}
Output
Going to calculate the area of the square
Enter the length of the side in meters: 10
The area of the square: 100.000000
3. Example for Function with argument and without return value
Example 1
#include<stdio.h>
void sum(int, int);
void main()
{
int a,b,result;
printf("\nGoing to calculate the sum of two numbers:");
printf("\nEnter two numbers:");
scanf("%d %d",&a,&b);
sum(a,b);
}
void sum(int a, int b)
{
printf("\nThe sum is %d",a+b);
}
Output
Going to calculate the sum of two numbers:

Enter two numbers 10


24

The sum is 34
Example 2: program to calculate the average of five numbers.
#include<stdio.h>
void average(int, int, int, int, int);
void main()
{
int a,b,c,d,e;
printf("\nGoing to calculate the average of five numbers:");
printf("\nEnter five numbers:");
scanf("%d %d %d %d %d",&a,&b,&c,&d,&e);
average(a,b,c,d,e);
}
void average(int a, int b, int c, int d, int e)
{
float avg;
avg = (a+b+c+d+e)/5;
printf("The average of given five numbers : %f",avg);
}
Output
Going to calculate the average of five numbers:
Enter five numbers:10
20
30
40
50
The average of given five numbers : 30.000000
4. Example for Function with argument and with return value
Example 1
#include<stdio.h>
int sum(int, int);
void main()
{
int a,b,result;
printf("\nGoing to calculate the sum of two numbers:");
printf("\nEnter two numbers:");
scanf("%d %d",&a,&b);
result = sum(a,b);
printf("\nThe sum is : %d",result);
}
int sum(int a, int b)
{
return a+b;
}
Output
Going to calculate the sum of two numbers:
Enter two numbers:10
20
The sum is : 30
Example 2: Program to check whether a number is even or odd
#include<stdio.h>
int even_odd(int);
void main()
{
int n,flag=0;
printf("\nGoing to check whether a number is even or odd");
printf("\nEnter the number: ");
scanf("%d",&n);
flag = even_odd(n);
if(flag == 0)
{
printf("\nThe number is odd");
}
else
{
printf("\nThe number is even");
}
}
int even_odd(int n)
{
if(n%2 == 0)
{
return 1;
}
else
{
return 0;
}
}
Output
Going to check whether a number is even or odd
Enter the number: 100
The number is even

C Library Functions
Library functions are the inbuilt function in C that are grouped and placed at a
common place called the library. Such functions are used to perform some specific operations.
For example, printf is a library function used to print on the console. The library functions are
created by the designers of compilers. All C standard library functions are defined inside the
different header files saved with the extension .h. We need to include these header files in our
program to make use of the library functions defined in such header files. For example, To use
the library functions such as printf/scanf we need to include stdio.h in our program which is a
header file that contains all the library functions regarding standard input/output.

The list of mostly used header files is given in the following table.

S Header Description
N file

1 stdio.h This is a standard input/output header file. It contains all the library
functions regarding standard input/output.

2 conio.h This is a console input/output header file.

3 string.h It contains all string related library functions like gets(), puts(),etc.

4 stdlib.h This header file contains all the general library functions like malloc(),
calloc(), exit(), etc.
5 math.h This header file contains all the math operations related functions like
sqrt(), pow(), etc.

6 time.h This header file contains all the time-related functions.

7 ctype.h This header file contains all character handling functions.

8 stdarg.h Variable argument functions are defined in this header file.

9 signal.h All the signal handling functions are defined in this header file.

10 setjmp.h This file contains all the jump functions.

11 locale.h This file contains locale functions.

12 errno.h This file contains error handling functions.

13 assert.h This file contains diagnostics functions.

Call by value and Call by reference in C


There are two methods to pass the data into the function in C language, i.e., call by
value and call by reference.

Call by value in C
● In call by value method, the value of the actual parameters is copied into the formal
parameters. In other words, we can say that the value of the variable is used in the
function call in the call by value method.
● In call by value method, we can not modify the value of the actual parameter by the
formal parameter.
● In call by value, different memory is allocated for actual and formal parameters since the
value of the actual parameter is copied into the formal parameter.
● The actual parameter is the argument which is used in the function call whereas formal
parameter is the argument which is used in the function definition.
Let's try to understand the concept of call by value in c language by the example given below:
#include<stdio.h>
void change(int num) {
printf("Before adding value inside function num=%d \n",num);
num=num+100;
printf("After adding value inside function num=%d \n", num);
}
int main() {
int x=100;
printf("Before function call x=%d \n", x);
change(x);//passing value in function
printf("After function call x=%d \n", x);
return 0;
}
Output
Before function call x=100
Before adding value inside function num=100
After adding value inside function num=200
After function call x=100

Call by Value Example: Swapping the values of the two variables


#include <stdio.h>
void swap(int , int); //prototype of the function
int main()
{
int a = 10;
int b = 20;
printf("Before swapping the values in main a = %d, b = %d\n",a,b); // printing the value of a a
nd b in main
swap(a,b);
printf("After swapping values in main a = %d, b = %d\n",a,b); // The value of actual parameter
s do not change by changing the formal parameters in call by value, a = 10, b = 20
}
void swap (int a, int b)
{
int temp;
temp = a;
a=b;
b=temp;
printf("After swapping values in function a = %d, b = %d\n",a,b); // Formal parameters, a = 20
, b = 10
}
Output
Before swapping the values in main a = 10, b = 20
After swapping values in function a = 20, b = 10
After swapping values in main a = 10, b = 20

Call by reference in C
● In call by reference, the address of the variable is passed into the function call as the
actual parameter.
● The value of the actual parameters can be modified by changing the formal parameters
since the address of the actual parameters is passed.
● In call by reference, the memory allocation is similar for both formal parameters and
actual parameters. All the operations in the function are performed on the value stored at
the address of the actual parameters, and the modified value gets stored at the same
address.

Consider the following example for the call by reference.


#include<stdio.h>
void change(int *num) {
printf("Before adding value inside function num=%d \n",*num);
(*num) += 100;
printf("After adding value inside function num=%d \n", *num);
}
int main() {
int x=100;
printf("Before function call x=%d \n", x);
change(&x);//passing reference in function
printf("After function call x=%d \n", x);
return 0;
}
Output
Before function call x=100
Before adding value inside function num=100
After adding value inside function num=200
After function call x=200
Call by reference Example: Swapping the values of the two variables
#include <stdio.h>
void swap(int *, int *); //prototype of the function
int main()
{
int a = 10;
int b = 20;
printf("Before swapping the values in main a = %d, b = %d\n",a,b); // printing the value of a a
nd b in main
swap(&a,&b);
printf("After swapping values in main a = %d, b = %d\n",a,b); // The values of actual paramet
ers do change in call by reference, a = 10, b = 20
}
void swap (int *a, int *b)
{
int temp;
temp = *a;
*a=*b;
*b=temp;
printf("After swapping values in function a = %d, b = %d\n",*a,*b); // Formal parameters, a =
20, b = 10
}
Output
Before swapping the values in main a = 10, b = 20
After swapping values in function a = 20, b = 10
After swapping values in main a = 20, b = 10

Difference between call by value and call by reference in c


No Call by value Call by reference
.

1 A copy of the value is passed into the function An address of value is passed into the function

2 Changes made inside the function are limited to Changes made inside the function validate
the function only. The values of the actual outside of the function also. The values of the
parameters do not change by changing the actual parameters do change by changing the
formal parameters. formal parameters.

3 Actual and formal arguments are created at the Actual and formal arguments are created at the
different memory location same memory location

Math Functions
There is also a list of math functions available, that allows you to perform mathematical tasks on
numbers.
To use them, you must include the math.h header file in your program:
#include <math.h>
1.Square Root
To find the square root of a number, use the sqrt() function:
Example
printf("%f", sqrt(16));
2.Round a Number
The ceil() function rounds a number upwards to its nearest integer, and the floor() method rounds
a number downwards to its nearest integer, and returns the result:
Example
printf("%f", ceil(1.4));
printf("%f", floor(1.4));
3.Power
The pow() function returns the value of x to the power of y (xy):
Example printf("%f", pow(4, 3));
Other Math Functions
A list of other popular math functions (from the <math.h> library) can be found in the table
below:

Function Description

abs(x) Returns the absolute value of x

acos(x) Returns the arccosine of x

asin(x) Returns the arcsine of x

atan(x) Returns the arctangent of x

cbrt(x) Returns the cube root of x

cos(x) Returns the cosine of x

exp(x) Returns the value of Ex

sin(x) Returns the sine of x (x is in radians)

tan(x) Returns the tangent of an angle

String Functions
There are many important string functions defined in "string.h" library.

No Function Description
.

1) strlen(string_name) returns the length of string name.

2) strcpy(destination, source) copies the contents of source string to destination string.

3) strcat(first_string, concats or joins first string with second string. The result of the
second_string) string is stored in first string.

4) strcmp(first_string, compares the first string with second string. If both strings are
second_string) same, it returns 0.

5) strrev(string) returns reverse string.

6) strlwr(string) returns string characters in lowercase.

7) strupr(string) returns string characters in uppercase.

Recursion in C
Recursion is the process which comes into existence when a function calls a copy of itself to
work on a smaller problem. Any function which calls itself is called recursive function, and such
function calls are called recursive calls. Recursion involves several numbers of recursive calls.
However, it is important to impose a termination condition of recursion. Recursion code is
shorter than iterative code however it is difficult to understand.

Recursion cannot be applied to all the problem, but it is more useful for the tasks that can be
defined in terms of similar subtasks. For Example, recursion may be applied to sorting,
searching, and traversal problems.

Generally, iterative solutions are more efficient than recursion since function call is always
overhead. Any problem that can be solved recursively, can also be solved iteratively. However,
some problems are best suited to be solved by the recursion, for example, tower of Hanoi,
Fibonacci series, factorial finding, etc.
In the following example, recursion is used to calculate the factorial of a number.s
#include <stdio.h>
int fact (int);
int main()
{
int n,f;
printf("Enter the number whose factorial you want to calculate?");
scanf("%d",&n);
f = fact(n);
printf("factorial = %d",f);
}
int fact(int n)
{
if (n==0)
{
return 0;
}
else if ( n == 1)
{
return 1;
}
else
{
return n*fact(n-1);
}
}
Output
Enter the number whose factorial you want to calculate?5
factorial = 120
We can understand the above program of the recursive method call by the figure given below:
Recursive Function
A recursive function performs the tasks by dividing it into the subtasks. There is a termination
condition defined in the function which is satisfied by some specific subtask. After this, the
recursion stops and the final result is returned from the function.

The case at which the function doesn't recur is called the base case whereas the instances where
the function keeps calling itself to perform a subtask, is called the recursive case. All the
recursive functions can be written using this format.

Pseudocode for writing any recursive function is given below.


if (test_for_base)
{
return some_value;
}
else if (test_for_another_base)
{
return some_another_value;
}
else
{
// Statements;
recursive call;
}
Example of recursion in C
Let's see an example to find the nth term of the Fibonacci series.
#include<stdio.h>
int fibonacci(int);
void main ()
{
int n,f;
printf("Enter the value of n?");
scanf("%d",&n);
f = fibonacci(n);
printf("%d",f);
}
int fibonacci (int n)
{
if (n==0)
{
return 0;
}
else if (n == 1)
{
return 1;
}
else
{
return fibonacci(n-1)+fibonacci(n-2);
}
}
Output
Enter the value of n?12
144

Memory allocation of Recursive method


Each recursive call creates a new copy of that method in the memory. Once some data is returned
by the method, the copy is removed from the memory. Since all the variables and other stuff
declared inside function get stored in the stack, therefore a separate stack is maintained at each
recursive call. Once the value is returned from the corresponding function, the stack gets
destroyed. Recursion involves so much complexity in resolving and tracking the values at each
recursive call. Therefore we need to maintain the stack and track the values of the variables
defined in the stack.
Let us consider the following example to understand the memory allocation of the recursive
functions.
int display (int n)
{
if(n == 0)
return 0; // terminating condition
else
{
printf("%d",n);
return display(n-1); // recursive call
}
}
Explanation
Let us examine this recursive function for n = 4. First, all the stacks are maintained which prints
the corresponding value of n until n becomes 0, Once the termination condition is reached, the
stacks get destroyed one by one by returning 0 to its calling stack. Consider the following image
for more information regarding the stack trace for the recursive functions.

Binary Search (Recursive and Iterative) in C Program


● Binary Search is a search algorithm that is used to find the position of an element
(target value ) in a sorted array. The array should be sorted prior to applying a binary
search.
● Binary search is also known by these names, logarithmic search, binary chop, half
interval search.

Working

The binary search algorithm works by comparing the element to be searched by the middle
element of the array and based on this comparison follows the required procedure.
Case 1
Case 2

Case 3

ALGORITHM

Parameters inital_value , end_value


Step 1 : Find the middle element of array. using ,
middle = initial_value + end_value / 2 ;
Step 2 : If middle = element, return ‘element found’ and index.
Step 3 : if middle > element, call the function with end_value = middle - 1 .
Step 4 : if middle < element, call the function with start_value = middle + 1 .
Step 5 : exit.

The implementation of the binary search algorithm function uses the call to function again and again. This call c

● Iterative
● Recursive

Iterative call is looping over the same block of code multiple times
Recursive call is calling the same function again and again.

PROGRAM TO IMPLEMENT BINARY SEARCH USING ITERATIVE CALL

Example
#include <stdio.h>
int iterativeBinarySearch(int array[], int start_index, int end_index, int element){
while (start_index <= end_index){
int middle = start_index + (end_index- start_index )/2;
if (array[middle] == element)
return middle;
if (array[middle] < element)
start_index = middle + 1;
else
end_index = middle - 1;
}
return -1;
}
int main(void){
int array[] = {1, 4, 7, 9, 16, 56, 70};
int n = 7;
int element = 16;
int found_index = iterativeBinarySearch(array, 0, n-1, element);
if(found_index == -1 ) {
printf("Element not found in the array ");
}
else {
printf("Element found at index : %d",found_index);
}
return 0;
}
Output
Element found at index : 4

PROGRAM TO IMPLEMENT BINARY SEARCH USING RECURSIVE CALL

Example
#include <stdio.h>
int recursiveBinarySearch(int array[], int start_index, int end_index, int element){
if (end_index >= start_index){
int middle = start_index + (end_index - start_index )/2;
if (array[middle] == element)
return middle;
if (array[middle] > element)
return recursiveBinarySearch(array, start_index, middle-1, element);
return recursiveBinarySearch(array, middle+1, end_index, element);
}
return -1;
}
int main(void){
int array[] = {1, 4, 7, 9, 16, 56, 70};
int n = 7;
int element = 9;
int found_index = recursiveBinarySearch(array, 0, n-1, element);
if(found_index == -1 ) {
printf("Element not found in the array ");
}
else {
printf("Element found at index : %d",found_index);
}
return 0;
}

C Pointers
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 type integ
er.
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
An example of using pointers to print the address and value is given below.
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.
Let's see the pointer example as explained for the above figure.
#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); // p contains the address of the number therefore printi
ng p gives the address of number.
printf("Value of p variable is %d \n",*p); // As we know that * is used to dereference a pointer th
erefore if we print *p, we will get the value stored at the address contained by p.
return 0;
}
Output
Address of number variable is fff4
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 array 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
There are many applications of pointers in c language.
1) Dynamic memory allocation
In c language, we can dynamically allocate memory using malloc() and calloc() functions where
the pointer is used.
2) Arrays, Functions, and Structures
Pointers in c language are widely used in arrays, functions, and structures. It reduces the code
and improves the performance.
Address Of (&) Operator
The address of operator '&' returns the address of a variable. But, we need to use %u to display
the address of a variable.
#include<stdio.h>
int main(){
int number=50;
printf("value of number is %d, address of number is %u",number,&number);
return 0;
}
Output
value of number is 50, address of number is fff4

NULL Pointer
A pointer that is not assigned any value but NULL is known as the NULL pointer. If you don't
have any address to be specified in the pointer at the time of declaration, you can assign NULL
value. It will provide a better approach.
int *p=NULL;
In the most libraries, the value of the pointer is 0 (zero).

Pointer Program to swap two numbers without using the 3rd variable.
#include<stdio.h>
int main(){
int a=10,b=20,*p1=&a,*p2=&b;

printf("Before swap: *p1=%d *p2=%d",*p1,*p2);


*p1=*p1+*p2;
*p2=*p1-*p2;
*p1=*p1-*p2;
printf("\nAfter swap: *p1=%d *p2=%d",*p1,*p2);

return 0;
}
Output
Before swap: *p1=10 *p2=20
After swap: *p1=20 *p2=10
Reading complex pointers
There are several things which must be taken into the consideration while reading the complex
pointers in C. Lets see the precedence and associativity of the operators which are used regarding
pointers.

Operator Precedence Associativity

(), [] 1 Left to right

*, identifier 2 Right to left

Data type 3 -

Here,we must notice that,


(): This operator is a bracket operator used to declare and define the function.
[]: This operator is an array subscript operator
* : This operator is a pointer operator.
Identifier: It is the name of the pointer. The priority will always be assigned to this.
Data type: Data type is the type of the variable to which the pointer is intended to point. It also
includes the modifier like signed int, long, etc).
How to read the pointer: int (*p)[10].
To read the pointer, we must see that () and [] have the equal precedence. Therefore, their
associativity must be considered here. The associativity is left to right, so the priority goes to ().
Inside the bracket (), pointer operator * and pointer name (identifier) p have the same
precedence. Therefore, their associativity must be considered here which is right to left, so the
priority goes to p, and the second priority goes to *.
Assign the 3rd priority to [] since the data type has the last precedence. Therefore the pointer will
look like following.
char -> 4
* -> 2
p -> 1
[10] -> 3
The pointer will be read as p is a pointer to an array of integers of size 10.
Example
How to read the following pointer?
int (*p)(int (*)[2], int (*)void))

Explanation
This pointer will be read as p is a pointer to such function which accepts the first parameter as
the pointer to a one-dimensional array of integers of size two and the second parameter as the
pointer to a function which parameter is void and return type is the integer.

The Pointer Operators


There are two pointer operators :
1. value at address operator ( * )
2. address of operator ( & )

Value at address operator ( * )


The * is a unary operator. It gives the value stored at a particular address. The ‘value at address’
operator is also called ‘indirection’ operator.
q = *m;
if m contains the memory address of the variable count, then preceding assignment statement can
places the value of count into q.

Address of operator ( & )


The & is a unary operator that returns the memory address of its operand.
m = & count;
The preceding assignment statement can be “The memory address of the variable count is places
into m”.
The following program that demonstrates the relationships between operators :
\* Pointer to initialize and print the value and address of variable. *\
# include < stdio.h >
int main( )
{
int a = 25 ;
int *b ;
b = &a ;
printf("\n Address of a = %u ", & a) ;
printf("\n Address of a = %u ", b) ;
printf("\n Address of b = %u ", & b) ;
printf("\n Value of b = %u ", b) ;
printf("\n Value of a = %d ", a) ;
printf("\n Value of a = %d ", *( &a ) )) ;
printf("\n Value of a = %d ", *b) ;
return ( 0 );}
Output of the program :
Address of a = 12345
Address of a = 12345
Address of b = 12345
Value of b = 12345
Value of a = 5
Value of a = 5
Value of a = 5
Pointer Arithmetics in C with Examples
Pointer Arithmetic is the set of valid arithmetic operations that can be performed on pointers.
The pointer variables store the memory address of another variable. It doesn’t store any value.
Hence, there are only a few operations that are allowed to perform on Pointers in C language.
The C pointer arithmetic operations are slightly different from the ones that we generally use for
mathematical calculations.

These operations are:


● Increment/Decrement of a Pointer
● Addition of integer to a pointer
● Subtraction of integer to a pointer
● Subtracting two pointers of the same type
● Comparison of pointers of the same type

1. Increment/Decrement of a Pointer
Increment: It is a condition that also comes under addition. When a pointer is incremented, it
actually increments by the number equal to the size of the data type for which it is a pointer.
Example:
If an integer pointer that stores address 1000 is incremented, then it will increment by 4(size of
an int), and the new address will point to 1004. While if a float type pointer is incremented then
it will increment by 4(size of a float) and the new address will be 1004.
Decrement: It is a condition that also comes under subtraction. When a pointer is decremented, it
actually decrements by the number equal to the size of the data type for which it is a pointer.
Example:
If an integer pointer that stores address 1000 is decremented, then it will decrement by 4(size of
an int), and the new address will point to 996. While if a float type pointer is decremented then it
will decrement by 4(size of a float) and the new address will be 996.

Note: It is assumed here that the architecture is 64-bit and all the data types are sized
accordingly. For example, integer is of 4 bytes.
Example of Pointer Increment and Decrement

#include <stdio.h>
// pointer increment and decrement
//pointers are incremented and decremented by the size of the data type they point to
int main()
{
int a = 22;
int *p = &a;
printf("p = %u\n", p); // p = 6422288
p++;
printf("p++ = %u\n", p); //p++ = 6422292 +4 // 4 bytes
p--;
printf("p-- = %u\n", p); //p-- = 6422288 -4 // restored to original value
float b = 22.22;
float *q = &b;
printf("q = %u\n", q); //q = 6422284
q++;
printf("q++ = %u\n", q); //q++ = 6422288 +4 // 4 bytes
q--;
printf("q-- = %u\n", q); //q-- = 6422284 -4 // restored to original value
char c = 'a';
char *r = &c;
printf("r = %u\n", r); //r = 6422283
r++;
printf("r++ = %u\n", r); //r++ = 6422284 +1 // 1 byte
r--;
printf("r-- = %u\n", r); //r-- = 6422283 -1 // restored to original value
return 0;
}

Output
p = 6422288
p++ = 6422292
p-- = 6422288
q = 6422284
q++ = 6422288
q-- = 6422284
r = 6422283
r++ = 6422284
r-- = 6422283
Note: Pointers can be outputted using %p, since, most of the computers store the address value
in hexadecimal form using %p gives the value in that form. But for simplicity and understanding
we can also use %u to get the value in Unsigned int form.

2. Addition of Integer to Pointer


When a pointer is added with an integer value, the value is first multiplied by the size of the data
type and then added to the pointer.
Example:
Consider the same example as above where the ptr is an integer pointer that stores 1000 as an
address. If we add integer 5 to it using the expression, ptr = ptr + 5, then, the final address stored
in the ptr will be ptr = 1000 + sizeof(int) * 5 = 1020.
Example of Addition of Integer to Pointer

// C program to illustrate pointer Addition


#include <stdio.h>

// Driver Code
int main()
{
// Integer variable
int N = 4;

// Pointer to an integer
int *ptr1, *ptr2;

// Pointer stores the address of N


ptr1 = &N;
ptr2 = &N;

printf("Pointer ptr2 before Addition: ");


printf("%p \n", ptr2);

// Addition of 3 to ptr2
ptr2 = ptr2 + 3;
printf("Pointer ptr2 after Addition: ");
printf("%p \n", ptr2);
return 0;
}

Output
Pointer ptr2 before Addition: 0x7ffca373da9c
Pointer ptr2 after Addition: 0x7ffca373daa8

3. Subtraction of Integer to Pointer


When a pointer is subtracted with an integer value, the value is first multiplied by the size of the
data type and then subtracted from the pointer similar to an addition.
Example:
Consider the same example as above where the ptr is an integer pointer that stores 1000 as an
address. If we subtract integer 5 from it using the expression, ptr = ptr – 5, then, the final address
stored in the ptr will be ptr = 1000 – sizeof(int) * 5 = 980.
Example of Subtraction of Integer from Pointer

// C program to illustrate pointer Subtraction


#include <stdio.h>

// Driver Code
int main()
{
// Integer variable
int N = 4;

// Pointer to an integer
int *ptr1, *ptr2;

// Pointer stores the address of N


ptr1 = &N;
ptr2 = &N;

printf("Pointer ptr2 before Subtraction: ");


printf("%p \n", ptr2);

// Subtraction of 3 to ptr2
ptr2 = ptr2 - 3;
printf("Pointer ptr2 after Subtraction: ");
printf("%p \n", ptr2);

return 0;
}

Output
Pointer ptr2 before Subtraction: 0x7ffd718ffebc
Pointer ptr2 after Subtraction: 0x7ffd718ffeb0

4. Subtraction of Two Pointers


The subtraction of two pointers is possible only when they have the same data type. The result is
generated by calculating the difference between the addresses of the two pointers and calculating
how many bits of data it is according to the pointer data type. The subtraction of two pointers
gives the increments between the two pointers.
Example:
Two integer pointers say ptr1(address:1000) and ptr2(address:1004) are subtracted. The
difference between addresses is 4 bytes. Since the size of int is 4 bytes, therefore the increment
between ptr1 and ptr2 is given by (4/4) = 1.
Below is the implementation to illustrate the Subtraction of Two Pointers:

// C program to illustrate Subtraction


// of two pointers
#include <stdio.h>

// Driver Code
int main()
{
int x = 6; // Integer variable declaration
int N = 4;

// Pointer declaration
int *ptr1, *ptr2;
ptr1 = &N; // stores address of N
ptr2 = &x; // stores address of x

printf(" ptr1 = %u, ptr2 = %u\n", ptr1, ptr2);


// %p gives an hexa-decimal value,
// We convert it into an unsigned int value by using %u

// Subtraction of ptr2 and ptr1


x = ptr1 - ptr2;

// Print x to get the Increment


// between ptr1 and ptr2
printf("Subtraction of ptr1 "
"& ptr2 is %d\n",
x);
return 0;
}

Output
ptr1 = 2715594428, ptr2 = 2715594424
Subtraction of ptr1 & ptr2 is 1
5. Comparison of pointers of the same type
We can compare the two pointers by using the comparison operators in C. We can implement
this by using all operators in C >, >=, <, <=, ==, !=. It returns true for the valid condition and
returns false for the unsatisfied condition.
Step 1: Initialize the integer values and point these integer values to the pointer.
Step 2: Now, check the condition by using comparison or relational operators on pointer
variables.
Step 3: Display the output.
Example of Pointer Comparision
// C Program to illustrate the pointer comparison
#include <stdio.h>
int main()
{
// code
int num1 = 5, num2 = 6, num3 = 5; // integer input
int* p1 = &num1; // addressing the integer input to pointer
int* p2 = &num2;
int* p3 = &num3;
// comparing the pointer variables.
if (*p1 < *p2) {
printf("\n%d less than %d", *p1, *p2);
}
if (*p2 > *p1) {
printf("\n%d greater than %d", *p2, *p1);
}
if (*p3 == *p1) {
printf("\nBoth the values are equal");
}
if (*p3 != *p2) {
printf("\nBoth the values are not equal");
}
return 0;
}

Output
5 less than 6
6 greater than 5
Both the values are equal
Both the values are not equal

Arrays and pointers – Array of pointers


In C, a pointer array is a homogeneous collection of indexed pointer variables that are
references to a memory location. It is generally used in C Programming when we want to point at
multiple memory locations of a similar data type in our C program. We can access the data by
dereferencing the pointer pointing to it.
Syntax:
pointer_type *array_name [array_size];

Here,
pointer_type: Type of data the pointer is pointing to.
array_name: Name of the array of pointers.
array_size: Size of the array of pointers.

Note: It is important to keep in mind the operator precedence and associativity in the array of
pointers declarations of different type as a single change will mean the whole different thing. For
example, enclosing *array_name in the parenthesis will mean that array_name is a pointer to an
array.
Example:

// C program to demonstrate the use of array of pointers


#include <stdio.h>

int main()
{
// declaring some temp variables
int var1 = 10;
int var2 = 20;
int var3 = 30;

// array of pointers to integers


int* ptr_arr[3] = { &var1, &var2, &var3 };

// traversing using loop


for (int i = 0; i < 3; i++) {
printf("Value of var%d: %d\tAddress: %p\n", i + 1, *ptr_arr[i], ptr_arr[i]);
}

return 0;
}

Output
Value of var1: 10 Address: 0x7fff1ac82484
Value of var2: 20 Address: 0x7fff1ac82488
Value of var3: 30 Address: 0x7fff1ac8248c
Explanation:

As shown in the above example, each element of the array is a pointer pointing to an integer. We
can access the value of these integers by first selecting the array element and then dereferencing
it to get the value.

Array of Pointers to Character


One of the main applications of the array of pointers is to store multiple strings as an
array of pointers to characters. Here, each pointer in the array is a character pointer that points to
the first character of the string.

Syntax:
char *array_name [array_size];
After that, we can assign a string of any length to these pointers.
Example:

char* arr[5]
= { "gfg", "geek", "Geek", "Geeks", "GeeksforGeeks" }

This method of storing strings has the advantage of the traditional array of strings. Consider the
following two examples:
Example 1:

// C Program to print Array of strings without array of pointers


#include <stdio.h>
int main()
{
char str[3][10] = { "Geek", "Geeks", "Geekfor" };

printf("String array Elements are:\n");

for (int i = 0; i < 3; i++) {


printf("%s\n", str[i]);
}

return 0;
}

Output
String array Elements are:
Geek
Geeks
Geekfor
In the above program, we have declared the 3 rows and 10 columns of our array of strings.
But because of predefining the size of the array of strings the space consumption of the program
increases if the memory is not utilized properly or left unused. Now let’s try to store the same
strings in an array of pointers.
Example 2

// C program to illustrate the use of array of pointers to


// characters
#include <stdio.h>
int main()
{
char* arr[3] = { "geek", "Geeks", "Geeksfor" };

for (int i = 0; i < 3; i++) {


printf("%s\n", arr[i]);
}
return 0;
}

Output
geek
Geeks
Geeksfor
Here, the total memory used is the memory required for storing the strings and
pointers without leaving any empty space hence, saving a lot of wasted space. We can
understand this using the image shown below.

The space occupied by the array of pointers to characters is shown by solid green blocks
excluding the memory required for storing the pointer while the space occupied by the array of
strings includes both solid and light green blocks.

Array of Pointers to Different Types


Not only we can define the array of pointers for basic data types like int, char, float, etc.
but we can also define them for derived and user-defined data types such as arrays, structures,
etc. Let’s consider the below example where we create an array of pointers pointing to a function
for performing the different operations.
Example:

// C program to illustrate the use of array of pointers to


// function
#include <stdio.h>

// some basic arithmetic operations


void add(int a, int b) {
printf("Sum : %d\n", a + b);
}

void subtract(int a, int b) {


printf("Difference : %d\n", a - b);
}

void multiply(int a, int b) {


printf("Product : %d\n", a * b);
}
void divide(int a, int b) {
printf("Quotient : %d", a / b);
}

int main() {

int x = 50, y = 5;

// array of pointers to function of return type int


void (*arr[4])(int, int)
= { &add, &subtract, &multiply, ÷ };
for (int i = 0; i < 4; i++) {
arr[i](x, y);
}
return 0;
}

Output
Sum : 55
Difference : 45
Product : 250
Quotient : 10

Application of Array of Pointers


An array of pointers is useful in a wide range of cases. Some of these applications are listed
below:
● It is most commonly used to store multiple strings.
● It is also used to implement Linked HashMap in C and also in the Chaining technique of
collision resolving in Hashing.
● It is used in sorting algorithms like bucket sort.
● It can be used with any pointer type so it is useful when we have separate declarations of
multiple entities and we want to store them in a single place.
Disadvantages of Array of Pointers
The array of pointers also has its fair share of disadvantages and should be used when the
advantages outweigh the disadvantages. Some of the disadvantages of the array of pointers are:
● Higher Memory Consumption: An array of pointers requires more memory as
compared to plain arrays because of the additional space required to store pointers.
● Complexity: An array of pointers might be complex to use as compared to a simple
array.
● Prone to Bugs: As we use pointers, all the bugs associated with pointers come with it so
we need to handle them carefully.

Parameter passing: Pass by value, Pass by reference.


Functions can be invoked in two ways: Call by Value or Call by Reference.
These two ways are generally differentiated by the type of values passed to them as parameters.
The parameters passed to the function are called actual parameters whereas the parameters
received by the function are called formal parameters.
1.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 of the
caller.
Example of Call by Value

// C program to illustrate call by value


#include <stdio.h>
// Function Prototype
void swapx(int x, int y);
// Main function
int main()
{
int a = 10, b = 20;
// Pass by Values
swapx(a, b);

printf("In the Caller:\na = %d b = %d\n", a, b);


return 0;
}
// Swap functions that swaps
// two values
void swapx(int x, int y)
{
int t;
t = x;
x = y;
y = t;
printf("Inside Function:\nx = %d y = %d\n", x, y);
}

Output
Inside Function:
x = 20 y = 10
In the Caller:
a = 10 b = 20 // Thus actual values of a and b remain unchanged even after exchanging the
values of x and y in the function.
2.Call by Reference
● In call by reference method of parameter passing, the address of the actual parameters is
passed to the function as the formal parameters.
● Both the actual and formal parameters refer to the same locations.
● Any changes made inside the function are actually reflected in the actual parameters of
the caller.
Example of Call by Reference

// C program to illustrate Call by Reference


#include <stdio.h>

// Function Prototype
void swapx(int*, int*);

// Main function
int main()
{
int a = 10, b = 20;

// Pass reference
swapx(&a, &b);

printf("Inside the Caller:\na = %d b = %d\n", a, b);

return 0;
}

// Function to swap two variables


// by references
void swapx(int* x, int* y)
{
int t;

t = *x;
*x = *y;
*y = t;

printf("Inside the Function:\nx = %d y = %d\n", *x, *y);


}

Output
Inside the Function:
x = 20 y = 10
Inside the Caller:
a = 20 b = 10
Thus actual values of a and b get changed after exchanging values of x and y.

Difference between the Call by Value and Call by Reference


The following table lists the differences between the call-by-value and call-by-reference methods
of parameter passing.
Call By Value Call By Reference

While calling a function, instead of passing the


While calling a function, we pass the
values of variables, we pass the address of
values of variables to it. Such functions are
variables(location of variables) to the function
known as “Call By Values”.
known as “Call By References.

In this method, the value of each variable


In this method, the address of actual variables in
in the calling function is copied into
the calling function is copied into the dummy
corresponding dummy variables of the
variables of the called function.
called function.

With this method, the changes made to the


With this method, using addresses we would
dummy variables in the called function
have access to the actual variables and hence we
have no effect on the values of actual
would be able to manipulate them.
variables in the calling function.

In call-by-values, we cannot alter the


In call by reference, we can alter the values of
values of actual variables through function
variables through function calls.
calls.

Values of variables are passed by the Pointer variables are necessary to define to store
Simple technique. the address values of variables.

This method is preferred when we have to


This method is preferred when we have to pass a
pass some small values that should not
large amount of data to the function.
change.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy