What is
Pointer Arithmetics?
Image:https://hackingcpp.com/cpp/lang/pointer_arithmetic.html
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. There are
only a few operations that are allowed to perform on
Pointers in C language.
Image:https://www.geeksforgeeks.org/c/pointer-arithmetics-in-c-with-examples/
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.
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.
Image:https://www.geeksforgeeks.org/c/pointer-arithmetics-in-c-with-examples/
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.
Image:https://www.geeksforgeeks.org/c/pointer-arithmetics-in-c-with-examples/
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
addition.
Image:https://www.geeksforgeeks.org/c/pointer-arithmetics-in-c-with-examples/
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 bytes of data it is
according to the pointer data type. The subtraction of two
pointers gives the increments between the two pointers.
Image:https://www.geeksforgeeks.org/relational-operators-in-c/
5. Comparison of Pointers
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.
Learn Smart Manufacturing
& AI Skills for Industry 4.0
Learn new digital manufacturing skills such as Industry
4.0, IIoT, Robotics, Additive Manufacturing, Digital Twins,
PLC, Material Informatics and Python/C/C++ for core
engineering using AI tools.
Inspiring engineers to
innovate for Industry 4.0.
IndustryX.ai offers practical, industry-ready courses on
Smart Manufacturing, AI, and digital technologies —
inspiring engineers to innovate for Industry 4.0.
Recommended for new-age Mechatronics, Automation,
and Industrial Engineering students and professionals.