C supports a rich set of built-in operations like arithmetic, relational, assignment, conditional, etc. which can be performed on identifiers. Just like any other variable, these operations can be also performed on pointer variables .
Arithmetic Operators
We can perform arithmetic operations to pointer variables using arithmetic operators. We can add an integer or subtract an integer using a pointer pointing to that integer variable. The given table shows the arithmetic operators that can be performed on pointer variables:
Examples:
*ptr1 + *ptr2
*ptr1 * *ptr2
*ptr1 + *ptr2 - *ptr3
We can also directly perform arithmetic expressions on integers by dereferencing pointers. Let’s look at the example given below where p1 and p2 are pointers.
*p1 + 10, *p2 - 5, *p1 - *p2 + 10, *p1/2
Below diagram represents how exactly the expression/operators work with pointers. As seen in the diagram, pointers ‘pa’ and ‘pb’ point to integer variables ‘a’ and ‘b’ respectively. The addition is performed directly between integer variables and pointer variable and the results are stored in integer variable ‘c’ and ‘x’ respectively. Both the results are the same. Let us understand pointer arithmetic expression better with given code:
Addition = 30 Subtraction = 10 Multiplication = 200 Division = 2 Modulo = 0
Note: While performing division, make sure you put a blank space between ‘/’ and ‘*’ of the pointer as together it would make a multi-line comment(‘/*’). Example:
Incorrect: *ptr_a/*ptr_b;
Correct: *ptr_a / *ptr_b;
Correct: (*ptr_a)/(*ptr_b);
Relational Operators
Relational operations are often used to compare the values of the variable based on which we can take decisions. The given table shows the relational operators that can be performed on pointer variables. Example:
*ptr1 > *ptr2
*ptr1 < *ptr2
The value of the relational expression is either 0 or 1 that is false or true. The expression will return value 1 if the expression is true and it’ll return value 0 if false. Let us understand relational expression on pointer better with the code given below:
20 is greater than 10.
Output:
20 is greater than 10.
Assignment Operators
Assignment operators are used to assign values to the identifiers. There are multiple shorthand operations available. A table is given below showing the actual assignment statement with its shorthand statement.
Examples:
*a=10
*b+=20
*z=3.5
*s=4.56743
Let us understand assignment operator in better way with the help of code given below:
Value of variable a = 50
Conditional Operators
There is only one mostly used conditional operator in C known as Ternary operator. Ternary operator first checks the expression and depending on its return value returns true or false, which triggers/selects another expression. Syntax:
expression1 ? expression2 : expression3;
Example:
c = (*ptr1 > *ptr2) ? *ptr1 : *ptr2;
Let us understand the concept through the given code:
20 is the greatest.
Unary Operators
There are mainly two operators which are given as follows. Examples:
(*ptr1)++
(*ptr1)--
Let us understand the use of the unary operator through the given code:
Increment: Before increment a = 34 After increment a = 35 Decrement: Before decrement a = 35 After decrement a=34
Bitwise Operators
Binary operators are also known as bitwise operators. It is used to manipulate data at bit level. Bitwise operators can’t be used for float and double datatype. A table is shown below with all bitwise operators:
Examples:
*ptr1 & *ptr2
*ptr1 | *ptr2
*ptr1 ^ *ptr2
Let us understand the concept through the given code:
a AND b = 0 a OR b = 3 a Exclusive-OR b = 3