Table of contents
1.
Introduction
2.
What is an Indirection Operator?
2.1.
Example
2.2.
Implementation
2.3.
Output
2.4.
Explanation
3.
Limitations  of Indirection Operator (*)
4.
Frequently Asked Questions
4.1.
What is indirection operator in C?
4.2.
What is the example of indirection operator?
4.3.
What is indirection operator and dereference in C?
4.4.
Why '*' is called as the indirection operator?
4.5.
What is the use of (*) indirection operator?
5.
Conclusion
Last Updated: Dec 11, 2024
Medium

Indirection Operator (*) in C

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

The indirection operator is often denoted as “*”. It is crucial for working with pointers. indirection operator is a unary operator that allows you to access the value stored at the memory location held by a pointer variable. This concept is fundamental for tasks like dynamic memory allocation and passing parameters by reference.

Indirection Operator in C

This blog will try to understand the indirection operator in the C language. First, we understand the Operators in c. Then we go through the indirection operator in c language

What is an Indirection Operator?

Dereferencing operator is commonly referred to as the indirection operator in c in computer programming. It serves as a pointer's representation. An asterisk (*) indicates that this dereferencing operator is present. There is saved the address of the variable that a pointer variable points to. A dereferencing operator returns the value of the variable it is referring to when it is utilized.

asterisk (*) indicate

Example

In the language C, we can declare two variables, x, which store integer values, and p, which store pointers to integer values stored in memory: 


int x; int *p;

In this case, the compiler is informed by the asterisk that "p is not an integer, but rather a reference to a place in memory that stores an integer" (int x; int *p). Here, it is a component of a pointer declaration rather than a dereference. 

Now, we can use the & operator, which stands for "address of," to assign p to the place designated for the value of x. 

p = &x;

This step informs the compiler that the address you allocated for the integer x is the address that p refers to in memory. 

As an illustration, if we use the conventional method to set the value of x to 1, the outcome will be 1. and print the value. 

x = 1; printf("%d", x);

By referring to p, we may also alter the value of x. This is indicated by an asterisk: 

*p = 2; printf("%d", x);

The output then becomes 2. 

Try it by yourself with a free online editor.

Implementation

/* Dereferencing Operators are explained in a C programme */

 #include <stdio.h>
 
// Driver code
int main() {
//assigning variable t as 5.
    int t=5;
    int *ptr;
    
    ptr=&t;
    *ptr=7;
    printf("Value of the variable is %d", t);
    
    return 0;
}

Output

Value of the variable is 7

Explanation

Let's examine the software in question line by line: 

  • The variable that will be pointed at and the pointer is initially defined. ((int t=5, int *ptr);)) 
  • The pointer variable (ptr=&t;) is then used to hold the variable's address. 
  • The dereferenced pointer (*ptr=7;) can change the variable's value.

Also see, Tribonacci Series  and Short int in C Programming

Limitations  of Indirection Operator (*)

The indirection operator in c has the following restrictions: 

  • Only number pointers can be utilized with the indirection operator in c. 
  • It is impossible to allocate a value to a memory location using the indirection operator in c. 
  • When passing an argument to a function whose parameter has been specified using the BYREF operator, you cannot use the indirection operator in c. 
  • A pointer cannot be declared using the indirection operator in C. 
  • In a function declaration, a variable parameter cannot be declared using the indirection operator in c.

Also See, Sum of Digits in C, C Static Function  and,  Unary operator overloading in c++.

Frequently Asked Questions

What is indirection operator in C?

A pointer variable refers to the value kept at the memory location. It can be obtained using the indirection operator. This is a unary operator. There cannot be a space between the indirection operator and the name of the pointer variable.

What is the example of indirection operator?

The value kept at the memory location referred to by a pointer variable can be obtained using the indirection operator. It is a unary operator. An example of indirection operator is num_value = *p_num; Another example of indirection is int a = 10, b; int *p-Int = &a; b = *p_Int;

What is indirection operator and dereference in C?

The indirection operator can be used with a pointer to an integer, a single-dimensional array of pointers to integers, a pointer to a char, and a pointer to an unknown type. Dereference operators are another name for indirection operators.

Why '*' is called as the indirection operator?

An indirection operator, also referred to as a dereference operator, functions on a pointer variable. It gives back the memory location value, or l-value, that the variable's value pointed to. The symbol for the deference operator is an asterisk (*).

What is the use of (*) indirection operator?

The value referred to by the pointer-type operand is identified by the * (indirection) operator. A pointer to an incomplete type cannot be the operand. The operation produces a lvalue pointing to the object if the operand points to an object.

Conclusion

In this blog, we have learned about the Indirection operator in C. Addition, and we have gone through its example and proper code implementation. And at last, we have also covered the limitation of the Indirection operator in c.

 

Live masterclass