Address Operator (&) And Indirection Operator(*) In C Language
Address Operator (&) And indirection operator(*)
Address Operator (&) And indirection operator(*) is the most important concept to understand pointer because in C language when we use pointer then both these concepts will be used.
So without wasting time let’s learn about Address Operator (&) And indirection operator(*).
Address Operator (&) In C
In C language, by looking variable declaration, we can easily tell these three things – the name of the variable, what type of value will be stored in the variable, and what size value will be stored in the variable. But there is one more thing that the variable gets in memory and that is the memory address.
The place where the variable gets the memory has an address called memory address. The memory address of the variable is not in our hands, it is decided by the system.
If we want to know the memory address of a variable, then for this we have to use the address operator (&).
Address (&) is an operator that provides the address of a variable. It is a unary operator that operates on only one operand. This operand can only be a variable name, we cannot use it with a constant value.
We can easily identify that variable by the variable address provided by the Address Operator (&). Address operator (&) is also called referencing operator.
Syntax -:
&variable_name;
Example -:
#include<stdio.h>
void main()
{
int x = 5;
printf(" %d \n ", x );
printf("%d \n", &x);
}
In this example, we declared a variable named x by doing int x = 5 and assigned 5 to that variable at the time of declaration. After this, with the help of the printf() function, we first printed the value of x, after that the memory address of the place where the x variable got the memory was printed, whose output was something like this.
Output -:
5
200071
Asterisk (*) or Indirection operator In C
Asterisk (*) is an operator that returns the value stored in a variable address. It is a unary operator that operates on only one operand. Indirection operator (*) takes the address of the variable as an argument. It is also called dereferencing operator.
Syntax -:
*&variable_name;
Example -:
#include<stdio.h>
void main()
{
int x =5;
int y = *&x;
printf(" y = %d ", y);
}
In this example, we declared a variable named x by doing int x = 5; and assigned 5 to that variable at the time of declaration. After this we created another int variable named y and by doing *&x; in this y variable, whatever value is in the address of the x variable, it is assigned to the y variable. Here *x returns the value stored in the address of the variable.
With the help of printf() function, we first assigned the value to the y variable and printed the value on the screen, whose output was something like this.
Output -: y = 5
Let us see another example to understand the Address Operator (&) and the indirection operator better -:
Example of Address Operator (&) And indirection operator
#include<stdio.h>
void main()
{
int x = 5;
printf(" %d \n ", x );
printf("%d \n", &x);
printf(" %d " *&x);
}
Output
5
2048
5
In this example, we created a variable named x and assigned 5 to it, after that with the help of the first printf() we printed the store value in the x variable, and with the help of the second one printf() the address of the x variable Printed in screen
In the third printf(), we passed the address of the x variable as an argument by doing *&x; in the indirection operator (*), and as we know that when passing the address of a variable in the indirection operator (*) then the indirection operator (*) ) returns the value stored in that variable address, so with the help of third printf(), we got 5 values as output.
Friends, now you must have understood very well that what is Address Operator (&) And indirection operator(*) and how to use them in C language.
Address Operator (&) And indirection operator(*) is most commonly used with pointer, so after that, you should read about pointer.
You can read about pointer here -: What is a pointer?
Conclusion
Friends, I hope you have found the answer to your question and you will not have to search about What is Address Operator (&) And Indirection Operator(*) In C Language and How to use Address Operator (&) And Indirection Operator(*) In C Language
However, if you want any information related to this post or related to programming language, or computer science, then comment below I will clear your all doubts
If you want a complete tutorial on C language, then see here C Language Tutorial. Here you will get all the topics of C Programming Tutorial step by step.
Friends, if you liked this post, then definitely share this post with your friends so that they can get information about Address Operator And Indirection Operator In C Language
To get the information related to Programming Language, Coding, C, C ++, subscribe to our website newsletter. So that you will get information about our upcoming new posts soon.