Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
What is an Indirection Operator?
1.1.
Example
1.2.
Implementation
1.3.
Output
1.4.
Explanation
2.
Limitations  of Indirection Operator (*)
3.
Frequently Asked Questions
3.1.
What is indirection operator in C?
3.2.
What is the example of indirection operator?
3.3.
What is indirection operator and dereference in C?
3.4.
Why '*' is called as the indirection operator?
3.5.
What is the use of (*) indirection operator?
4.
Conclusion
Last Updated: Jun 28, 2024
Medium

Indirection Operator (*) in C

Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @

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

Must Read Passing Arrays to Function in C

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

Must Read what is storage class in c

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

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.

Please refer to our guided pathways on Code studio to learn more about DSA, Competitive Programming, JavaScript, System Design, etc. Enroll in our courses, and use the accessible sample exams and questions as a guide. For placement preparations, look at the interview experiences and interview package.

If you find any problems, please comment. We will love to answer your questions.

Attempt our Online Mock Test Series on Code360 now!

 

Previous article
Bitwise Operators in C Programming
Next article
Operator Precedence and Associativity in C
Live masterclass