×

Pointer arithmetic in C

In the C programming language, a pointer is an address which stores a numeric value. Hence, a developer can perform several arithmetic operations on the same just as one does on other numerical values. In the concept of arithmetic pointers, four operators can be used such as ++, --, +, and -.

The compiler will allow the user to perform arithmetic operations on pointers which are stored as an alias to a numerical value. Conducting operators using the operators and pointer variables is referred to as an arithmetic pointer.

Since pointers store the address of other variables, they are termed as address data types. These addresses are the memory locations that will be assigned to specific variables, and it never stores any value.

As mentioned, there are few operations allowed within an arithmetic pointer, and these operations differ from those generally used in mathematics.

1. Increment of a pointer: it is a condition that comes under addition. Every time a pointer is incremented, it increments by the number equal to the size of the data type present.

E.g.:

If an integer pointer that stores address 1000 is incremented, then it will increment by 2(size of an int), and the new address will point to 1002. 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.

#include <stdio.h>
const int MAX = 3;
int main () 
{
int  var[] = {10, 100, 200};
int  i, *ptr;


/* let us have array address in pointer */
ptr = var;


for ( i = 0; i < MAX; i++) 
{
printf("Address of var[%d] = %x\n", i, ptr );
printf("Value of var[%d] = %d\n", i, *ptr );


/* move to the next location */
ptr++;
}


return 0;
}

Output:

Address of var[0] = 9f8312cc
Value of var[0] = 10
Address of var[1] = 9f8312d0
Value of var[1] = 100
Address of var[2] = 9f8312d4
Value of var[2] = 200

2. Decrement of a pointer: It is a condition that also comes under subtraction. When a pointer is decremented, it decrements by the number equal to the size of the data type for which it is a pointer.

E.g.:

If an integer pointer that stores address 1000 is decremented, then it will decrement by 2(size of an int), and the new address will point to 998. 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.

Address of var[0] = 9f8312cc
Value of var[0] = 10
Address of var[1] = 9f8312d0
Value of var[1] = 100
Address of var[2] = 9f8312d4
Value of var[2] = 200


2.	Decrement of a pointer: It is a condition that also comes under subtraction. When a pointer is decremented, it decrements by the number equal to the size of the data type for which it is a pointer.
E.g.:
If an integer pointer that stores address 1000 is decremented, then it will decrement by 2(size of an int), and the new address will point to 998. 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.
#include <stdio.h>
const int MAX = 3;
int main () 
{


int  var[] = {10, 100, 200};
int  i, *ptr;


/* let us have array address in pointer */
ptr = &var[MAX-1];


for ( i = MAX; i > 0; i--) 
{


printf("Address of var[%d] = %x\n", i-1, ptr );
printf("Value of var[%d] = %d\n", i-1, *ptr );


/* move to the previous location */
ptr--;
}


return 0;
}

Output:

Address of var[2] = 9f3bb4a4
Value of var[2] = 200
Address of var[1] = 9f3bb4a0
Value of var[1] = 100
Address of var[0] = 9f3bb49c
Value of var[0] = 10

3. Addition of an integer to a pointer: when the pointer is added with a value, this value is then multiplied by the size of the data type and is then added to the pointer.

E.g.:

#include <stdio.h>
int main()
{
	int N = 4;
	int *ptr1, *ptr2;
	ptr1 = &N;
	ptr2 = &N;


	printf("Pointer ptr2 before Addition: ");
	printf("%p \n", ptr2);


	ptr2 = ptr2 + 3;
	printf("Pointer ptr2 after Addition: ");
	printf("%p \n", ptr2);


	return 0;
}

Output:

Pointer ptr2 before Addition: 0x7ffeb89a3bf4 
Pointer ptr2 after Addition: 0x7ffeb89a3c00 

4. Subtraction of integer from a pointer: when the pointer is subtracted with a value, this value is then multiplied by the size of the data type and is then subtracted to the pointer.

E.g.:

#include <stdio.h>
int main()
{
int N = 4;
int *ptr1, *ptr2;
ptr1 = &N;
ptr2 = &N;


printf("Pointer ptr2 before Subtraction: ");
printf("%p \n", ptr2);


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


return 0;
}

Output:

Pointer ptr2 before Subtraction: 0x7fffb534a334 
Pointer ptr2 after Subtraction: 0x7fffb534a328 

5. Comparisons of pointers: pointers and other pointers can be compared using some operators known as relational operators such as ==, <, and >.

E.g.:

#include <stdio.h>
const int MAX = 3;
int main () 
{
int  var[] = {10, 100, 200};
int  i, *ptr;
ptr = var;
i = 0;


while ( ptr <= &var[MAX - 1] ) 
{
printf("Address of var[%d] = %x\n", i, ptr );
printf("Value of var[%d] = %d\n", i, *ptr );
ptr++;
i++;
}


return 0;
}

Output:

Address of var[0] = e7e7612c
Value of var[0] = 10
Address of var[1] = e7e76130
Value of var[1] = 100
Address of var[2] = e7e76134
Value of var[2] = 200

Related Topics

Armstrong program in C using function

In this article we will learn how to find Armstrong of a number using function in C. An Armstrong number is a three-digit number that is the sum of its separate...

1 minute read.

Infix to Postfix program in C

Infix Expression: In infix expression, an operator is placed between the two operands. Example: x + y, here operator + is placed between operands x and y. Postfix Expression: In...

3 minutes read.

Type Casting in C

Type casting is a way to convert a variable from one data type to another data type.Syntax: (type) expression;     Example: #include <stdio.h> int main() { int a,b; float sum; printf("Example of Type Casting\n"); printf("Enter any integer nos:"); scanf("%d",&a,&b); sum=a+b; printf("Sum: %f",sum); return 0; } Output Example...

1 minute read.

#include in C

In the C programming language, #include is another way of inferring a standard, or a user defines file into the application or program. The preprocessor of the programming standard generally...

4 minutes read.

Multithreading in C interview questions

Q1. What exactly is a thread? Ans: A thread is a brief sequence of instructions intended to be planned and carried out by the CPU apart from the parent process. Q2. Can...

4 minutes read.

abs() function in C

abs() function is a built-in function present in the stdlib.h header file. It returns an integer value. abs() function converts a negative value into a positive value. For example, if...

4 minutes read.

Bank Account System in C using File Handling

This tells about the creation of bank account system using the C language and handling of files in C. Approach Let us see the approaches and the functions how they are covering...

5 minutes read.

What is required in each C Program?

Each C program must require one function, i.e., main() function. It is because when we execute the C program, C compiler looks for the main() function, and from here only...

5 minutes read.

How to initialize array to zero in C

In this article, you will learn how to initialise an array to 0 in C In C, an array is declared as: char ZEROARRAY[2022]; The global scope changes at runtime to all zeros....

3 minutes read.

Derived Data Types in C

A variable in a program occupies some space in the computer's memory where some value is stored. Each variable in C has an associated data type. A value to be...

4 minutes read.

How to create a node in C?

A node is a small concept taken from the graph theory, or a node is a fundamental unit of a data structure, like a tree data structure. Every graph contains...

3 minutes read.

Structure in C

Why Structure came into the picture? In the actual world, we deal with entities that are collections of objects rather than tiny bits of data like integers, characters, floats, etc. So,...

4 minutes read.

C Program for Extended Euclidean algorithms

The Euclidean method simply computes the GCD (greatest common divisor) of two numbers, p and q. (Let's assume) The GCD of two integers is the greatest number that divides them both....

3 minutes read.

Pointer in C

Pointer is a variable that is used to contain the address of another variable. We can easily create a pointer in C language. Example: int *ptr Symbol Description & (ampersand sign) Address of an operator...

1 minute read.

Fibonacci series program in C using Recursion

In this tutorial, we’ll explore how to utilise recursion to build the Fibonacci sequence in C language. What does the term "Fibonacci Series" mean? The following number in a Fibonacci number series is...

2 minutes read.

Conditional Operator in C

In C programming language, the conditional operator (also known as the ternary operator) is a shorthand way of writing an if-else statement. The syntax of conditional operator in C is...

3 minutes read.

Deadlock Detection Program in C

What is Deadlock? A deadlock is a circumstance where a group in the process is halted because they are each holding onto resources while waiting for other methods to obtain them. Think...

5 minutes read.

Comma Operator in C

What is a comma operator? The comma operator in the C programming language has the least priority. The comma operator is essentially a binary operator that operates on the first operand...

4 minutes read.

Remove an element from an array in C

A collection of objects or pieces of the same data type stored in a single memory block is known as an array. A data structure called an array is used...

3 minutes read.

Dos.h Header File in C Language

Dos.h is a header file in C. Interrupt handling, sound generation, date and time functions, and other tasks can be performed using the functions in this library. This is exclusive to...

4 minutes read.