The Wayback Machine - https://web.archive.org/web/20240820030853/https://www.geeksforgeeks.org/pointer-expressions-in-c-with-examples/
Open In App

Pointer Expressions in C with Examples

Last Updated : 03 Jun, 2024
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Prerequisite: Pointers in C Pointers are used to point to address the location of a variable. A pointer is declared by preceding the name of the pointer by an asterisk(*). Syntax:

datatype *pointer_name;

When we need to initialize a pointer with variable’s location, we use ampersand sign(&) before the variable name. Example: 

C
// Declaration of integer variable
int var=10;

// Initialization of pointer variable
int *pointer=&var;

The ampersand (&) is used to get the address of a variable. We can directly find the location of any identifier by just preceding it with an ampersand(&) sign. Example: 

C
// This code prints the address of x
#include <stdio.h>

int main()
{
    int x = 10;

    // Prints address of x
    printf(
        "Address of variable x = %p", &x);

    return 0;
}

Output
Address of variable x = 0x7fff3b690fd4

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:

Arithmetic-Operations-on-Pointers-in-C1-copy


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: 

C
// Program showing pointer expressions
// during Arithmetic Operations
#include <stdio.h>

int main()
{
    // Integer variables
    int a = 20, b = 10;

    // Variables for storing arithmetic
    // operations solution
    int add, sub, div, mul, mod;

    // Pointer variables for variables
    // a and b
    int *ptr_a, *ptr_b;

    // Initialization of pointers
    ptr_a = &a;
    ptr_b = &b;

    // Performing arithmetic Operations
    // on pointers
    add = *ptr_a + *ptr_b;
    sub = *ptr_a - *ptr_b;
    mul = *ptr_a * *ptr_b;
    div = *ptr_a / *ptr_b;
    mod = *ptr_a % *ptr_b;

    // Printing values
    printf("Addition = %d\n", add);
    printf("Subtraction = %d\n", sub);
    printf("Multiplication = %d\n", mul);
    printf("Division = %d\n", div);
    printf("Modulo = %d\n", mod);
    return 0;
}

Output
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: 

C
// Program showing pointer expressions
// during Relational Operations
#include &lt;stdio.h&gt;
int main()
{
    // Initializing integer variables
    int a = 20, b = 10;

    // Declaring pointer variables
    int* ptr_a;
    int* ptr_b;

    // Initializing pointer variables
    ptr_a = &amp;a;
    ptr_b = &amp;b;

    // Performing relational operations
    // less than operator
    if (*ptr_a &lt; *ptr_b) {
        printf(
            &quot;%d is less than %d.&quot;, *ptr_a, *ptr_b);
    }

    // Greater than operator
    if (*ptr_a &gt; *ptr_b) {
        printf(
            &quot;%d is greater than %d.&quot;, *ptr_a, *ptr_b);
    }

    // Equal to
    if (*ptr_a == *ptr_b) {
        printf(
            &quot;%d is equal to %d.&quot;, *ptr_a, *ptr_b);
    }

    return 0;
}

Output
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.

Assignment-Operations-on-Pointers-in-C-copy


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: 

C
// Program showing pointer expressions
// during Assignment Operations
#include &lt;stdio.h&gt;
int main()
{
    // Initializing integer variable
    int a = 30;

    // Declaring pointer variable
    int* ptr_a;

    // Initializing pointer using
    // assignment operator
    ptr_a = &amp;a;

    // Changing the variable's value using
    // assignment operator
    *ptr_a = 50;

    // Printing value of 'a' after
    // updating its value
    printf(&quot;Value of variable a = %d&quot;, *ptr_a);

    return 0;
}

Output
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;
  • As shown in example, assuming *ptr1=20 and *ptr2=10 then the condition here becomes true for the expression, so it’ll return value of true expression i.e. *ptr1, so variable ‘c’ will now contain value of 20.
  • Considering same example, assume *ptr1=30 and *ptr2=50 then the condition is false for the expression, so it’ll return value of false expression i.e. *ptr2, so variable ‘c’ will now contain value 50.

Let us understand the concept through the given code: 

C
// Program showing pointer expressions
// during Conditional Operations
#include &lt;stdio.h&gt;
int main()
{
    // Initializing integer variables
    int a = 15, b = 20, result = 0;

    // Declaring pointer variables
    int *ptr_a, *ptr_b;

    // Initializing pointer variables
    ptr_a = &amp;a;
    ptr_b = &amp;b;

    // Performing ternary operator
    result = ((*ptr_a &gt; *ptr_b) ? *ptr_a : *ptr_b);

    // Printing result of ternary operator
    printf(&quot;%d is the greatest.&quot;, result);
    return 0;
}

Output
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: 

C
// Program showing pointer expressions
// during Unary Operations
#include &lt;stdio.h&gt;
int main()
{
    // Initializing integer variable
    int a = 34;

    // Declaring pointer variable
    int* ptr_a;

    // Initializing pointer variable
    ptr_a = &amp;a;

    // Value of a before increment
    printf(&quot;Increment:\n&quot;);
    printf(
        &quot;Before increment a = %d\n&quot;, *ptr_a);

    // Unary increment operation
    (*ptr_a)++;

    // Value of a after increment
    printf(
        &quot;After increment a = %d&quot;, *ptr_a);

    // Value before decrement
    printf(&quot;\n\nDecrement:\n&quot;);
    printf(
        &quot;Before decrement a = %d\n&quot;, *ptr_a);

    // unary decrement operation
    (*ptr_a)--;

    // Value after decrement
    printf(&quot;After decrement a=%d&quot;, *ptr_a);

    return 0;
}

Output
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: Bitwise-Operations-on-Pointers-in-C-copy


Examples:

*ptr1 & *ptr2
*ptr1 | *ptr2
*ptr1 ^ *ptr2

Let us understand the concept through the given code: 

C
// Program showing pointer expressions
// during Bitwise Operations
#include &lt;stdio.h&gt;
int main()
{
    // Declaring integer variable for
    // storing result
    int and, or, ex_or;

    // Initializing integer variable
    int a = 1, b = 2;

    // Performing bitwise operations
    // AND operation
    and = a &amp; b;

    // OR operation
    or = a | b;

    // EX-OR operation
    ex_or = a ^ b;

    // Printing result of operations
    printf(&quot;\na AND b = %d&quot;, and);
    printf(&quot;\na OR b = %d&quot;, or);
    printf(&quot;\na Exclusive-OR b = %d&quot;, ex_or);
    return 0;
}

Output
a AND b = 0
a OR b = 3
a Exclusive-OR b = 3


Similar Reads

C - Pointer to Pointer (Double Pointer)
Prerequisite: Pointers in C The pointer to a pointer in C is used when we want to store the address of another pointer. The first pointer is used to store the address of the variable. And the second pointer is used to store the address of the first pointer. That is why they are also known as double-pointers. We can use a pointer to a pointer to cha
5 min read
What is a Pointer to a Null pointer
NULL pointer in C At the very high level, we can think of NULL as a null pointer which is used in C for various purposes. Some of the most common use cases for NULL are To initialize a pointer variable when that pointer variable isn’t assigned any valid memory address yet. C/C++ Code int* pInt = NULL; To check for a null pointer before accessing an
2 min read
Difference between Dangling pointer and Void pointer
Dangling pointer: A pointer pointing to a memory location that has been deleted (or freed) is called a dangling pointer. There are three different ways where Pointer acts as a dangling pointer: By deallocating memoryFunction CallWhen the variable goes out of scope Void pointer: Void pointer is a specific pointer type – void * – a pointer that point
2 min read
Pointer to an Array | Array Pointer
Prerequisite: Pointers Introduction  Consider the following program:  [GFGTABS] C #include&lt;stdio.h&gt; int main() { int arr[5] = { 1, 2, 3, 4, 5 }; int *ptr = arr; printf(&quot;%p\n&quot;, ptr); return 0; } [/GFGTABS]In the above program, we have a pointer ptr that points to the 0th element of the array. Similarly, we can also declare a pointer
9 min read
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
10 min read
Difference between NULL pointer, Null character ('\0') and '0' in C with Examples
NULL Pointer: The integer constant zero(0) has different meanings depending upon it's used. In all cases, it is an integer constant with the value 0, it is just described in different ways. If any pointer is being compared to 0, then this is a check to see if the pointer is a null pointer. This 0 is then referred to as a null pointer constant. The
2 min read
Properties of Regular expressions
Regular expressions : It is a way of representing regular languages.The algebraic description for regular languages is done using regular expressions.They can define the same language that various forms of finite automata can describe.Regular expressions offer something that finite automata do not, i.e. it is a declarative way to express the string
5 min read
Design finite automata from regular expressions
Prerequisite - Finite automata, Regular expressions, grammar, and language. In this article, we will see some popular regular expressions and how we can convert them to finite automata (NFA and DFA). Let's discuss it one by one. Overview :Let a and b are input symbols and r is the regular expression. Now we have to design NFA as well as DFA for eac
3 min read
Posix Basic Regular Expressions
POSIX stands for Portable Operating System Interface. It defines a set of standard operating system interfaces based on the UNIX OS. These standards are specified by the IEEE (Institute of Electrical and Electronics Engineers) society which maintains the compatibility between different operating systems. POSIX systems are theorized in such a way th
4 min read
Regular expressions in C
Prerequisite: How to write Regular Expressions?A regular expression is a sequence of characters that is used to search pattern. It is mainly used for pattern matching with strings, or string matching, etc. They are a generalized way to match patterns with sequences of characters. It is used in every programming language like C++, Java, and Python.P
3 min read
Regular Expressions, Regular Grammar and Regular Languages
As discussed in Chomsky Hierarchy, Regular Languages are the most restricted types of languages and are accepted by finite automata. Regular Expressions Regular Expressions are used to denote regular languages. An expression is regular if: ? is a regular expression for regular language ?.? is a regular expression for regular language {?}.If a ? ? (
6 min read
Generalized Lambda Expressions in C++14
Lambda expressions were introduced in C++11. They are basically snippets of code that can be nested inside other functions and even function call statements. By combining a lambda expression with the auto keyword these expressions can then be later used in the program. We have discussed lambda expressions in detail in this article Lambda Expression
5 min read
How to declare a pointer to a function?
While a pointer to a variable or an object is used to access them indirectly, a pointer to a function is used to invoke a function indirectly. Well, we assume that you know what it means by a pointer in C. So how do we create a pointer to an integer in C? Huh..it is pretty simple... int *ptrInteger; /*We have put a * operator between int and ptrInt
2 min read
Pointer vs Array in C
Most of the time, pointer and array accesses can be treated as acting the same, the major exceptions being: 1. the sizeof operator sizeof(array) returns the amount of memory used by all elements in the array sizeof(pointer) only returns the amount of memory used by the pointer variable itself 2. the &amp; operator array is an alias for &amp;array[0
1 min read
NULL Pointer in C
The Null Pointer is the pointer that does not point to any location but NULL. According to C11 standard: “An integer constant expression with the value 0, or such an expression cast to type void *, is called a null pointer constant. If a null pointer constant is converted to a pointer type, the resulting pointer, called a null pointer, is guarantee
4 min read
C | Pointer Basics | Question 1
What is the output of following program? # include &lt;stdio.h&gt; void fun(int x) { x = 30; } int main() { int y = 20; fun(y); printf(&quot;%d&quot;, y); return 0; } (A) 30 (B) 20 (C) Compiler Error (D) Runtime Error Answer: (B) Explanation: Parameters are always passed by value in C. Therefore, in the above code, value of y is not modified using
1 min read
C | Pointer Basics | Question 2
Output of following program? # include &lt;stdio.h&gt; void fun(int *ptr) { *ptr = 30; } int main() { int y = 20; fun(&amp;y); printf(&quot;%d&quot;, y); return 0; } (A) 20 (B) 30 (C) Compiler Error (D) Runtime Error Answer: (B) Explanation: The function fun() expects a pointer ptr to an integer (or an address of an integer). It modifies the value
1 min read
C | Pointer Basics | Question 3
Output of following program? #include &lt;stdio.h&gt; int main() { int *ptr; int x; ptr = &amp;x; *ptr = 0; printf(&quot; x = %d\n&quot;, x); printf(&quot; *ptr = %d\n&quot;, *ptr); *ptr += 5; printf(&quot; x = %d\n&quot;, x); printf(&quot; *ptr = %d\n&quot;, *ptr); (*ptr)++; printf(&quot; x = %d\n&quot;, x); printf(&quot; *ptr = %d\n&quot;, *ptr);
2 min read
C | Pointer Basics | Question 4
Consider a compiler where int takes 4 bytes, char takes 1 byte and pointer takes 4 bytes. #include &lt;stdio.h&gt; int main() { int arri[] = {1, 2 ,3}; int *ptri = arri; char arrc[] = {1, 2 ,3}; char *ptrc = arrc; printf(&quot;sizeof arri[] = %d &quot;, sizeof(arri)); printf(&quot;sizeof ptri = %d &quot;, sizeof(ptri)); printf(&quot;sizeof arrc[] =
1 min read
C | Pointer Basics | Question 17
Assume that float takes 4 bytes, predict the output of following program. #include &lt;stdio.h&gt; int main() { float arr[5] = {12.5, 10.0, 13.5, 90.5, 0.5}; float *ptr1 = &amp;arr[0]; float *ptr2 = ptr1 + 3; printf(&quot;%f &quot;, *ptr2); printf(&quot;%d&quot;, ptr2 - ptr1); return 0; } (A) 90.500000 3 (B) 90.500000 12 (C) 10.000000 12 (D) 0.5000
1 min read
C | Pointer Basics | Question 6
#include&lt;stdio.h&gt; int main() { int arr[] = {10, 20, 30, 40, 50, 60}; int *ptr1 = arr; int *ptr2 = arr + 5; printf(&quot;Number of elements between two pointer are: %d.&quot;, (ptr2 - ptr1)); printf(&quot;Number of bytes between two pointers are: %d&quot;, (char*)ptr2 - (char*) ptr1); return 0; } Assume that an int variable takes 4 bytes and a
2 min read
C | Pointer Basics | Question 7
#include&lt;stdio.h&gt; int main() { int a; char *x; x = (char *) &amp;a; a = 512; x[0] = 1; x[1] = 2; printf(&quot;%d\n&quot;,a); return 0; } What is the output of above program? (A) Machine dependent (B) 513 (C) 258 (D) Compiler Error Answer: (A) Explanation: Output is 513 in a little endian machine. To understand this output, let integers be sto
1 min read
C | Pointer Basics | Question 8
C/C++ Code int main() { char *ptr = \"GeeksQuiz\"; printf(\"%c", *&*&*ptr); return 0; } (A) Compiler Error (B) Garbage Value (C) Runtime Error (D) G Answer: (D)Explanation: The operator * is used for dereferencing and the operator &amp; is used to get the address. These operators cancel out effect of each other when used one after another. We can a
1 min read
C | Advanced Pointer | Question 1
C/C++ Code void fun(int *p) { int q = 10; p = &q; } int main() { int r = 20; int *p = &r; fun(p); printf(\"%d\", *p); return 0; } (A) 10 (B) 20 (C) Compiler error (D) Runtime Error Answer: (B)Explanation: Inside fun(), q is a copy of the pointer p. So if we change q to point something else then p remains uneffected. If we want to change a local poi
2 min read
C | Advanced Pointer | Question 2
Assume sizeof an integer and a pointer is 4 byte. Output? #include &lt;stdio.h&gt; #define R 10 #define C 20 int main() { int (*p)[R][C]; printf(&quot;%d&quot;, sizeof(*p)); getchar(); return 0; } (A) 200 (B) 4 (C) 800 (D) 80 Answer: (C) Explanation: Output is 10*20*sizeof(int) which is “800″ for compilers with integer size as 4 bytes. When a point
1 min read
C | Pointer Basics | Question 17
#include&lt;stdio.h&gt; void fun(int arr[]) { int i; int arr_size = sizeof(arr)/sizeof(arr[0]); for (i = 0; i &lt; arr_size; i++) printf(&quot;%d &quot;, arr[i]); } int main() { int i; int arr[4] = {10, 20 ,30, 40}; fun(arr); return 0; } (A) 10 20 30 40 (B) Machine Dependent (C) 10 20 (D) Northing Answer: (B) Explanation: In C, array parameters are
2 min read
C | Pointer Basics | Question 10
The reason for using pointers in a C program is (A) Pointers allow different functions to share and modify their local variables. (B) To pass large structures so that complete copy of the structure can be avoided. (C) Pointers enable complex “linked\" data structures like linked lists and binary trees. (D) All of the above Answer: (D)Explanation: S
1 min read
C | Pointer Basics | Question 11
C/C++ Code #include void f(int *p, int *q) { p = q; *p = 2; } int i = 0, j = 1; int main() { f(&i, &j); printf(\"%d %d", i, j); getchar(); return 0; } (A) 2 2 (B) 2 1 (C) 0 1 (D) 0 2 Answer: (D)Explanation: See below f() with comments for explanation. /* p points to i and q points to j */ void f(int *p, int *q) { p = q; /* p also points to j now */
1 min read
C | Pointer Basics | Question 12
Consider this C code to swap two integers and these five statements after it: void swap(int *px, int *py) { *px = *px - *py; *py = *px + *py; *px = *py - *px; } S1: will generate a compilation error S2: may generate a segmentation fault at runtime depending on the arguments passed S3: correctly implements the swap procedure for all input pointers r
1 min read
C | Pointer Basics | Question 13
int f(int x, int *py, int **ppz) { int y, z; **ppz += 1; z = **ppz; *py += 2; y = *py; x += 3; return x + y + z; } void main() { int c, *b, **a; c = 4; b = &amp;c; a = &amp;b; printf(&quot;%d &quot;, f(c, b, a)); return 0; } (A) 18 (B) 19 (C) 21 (D) 22 Answer: (B) Explanation: Let us understand this line by line /* below line changes value of c to
1 min read
three90RightbarBannerImg