Updated April 6, 2023
Introduction to Address Operator in C
The Address Operator in C also called a pointer. This address operator is denoted by “&”. This & symbol is called an ampersand. This & is used in a unary operator. The purpose of this address operator or pointer is used to return the address of the variable. Once we declared a pointer variable, we have to initialize the pointer with a valid memory address; to get the memory address of the variable ampersand is used. When we use the ampersand symbol as a prefix to the variable name & and it gives the address of that variable. An address of the operator is used within C that is returned to the memory address of a variable. These addresses returned by the address of the operator are known as pointers because they “point” to the variable in memory.
Usage:
- While scanning the user input we used ampersand operator.
- While displaying the address of the variable we used ampersand operator.
Why Address Operator used in C?
Address operators commonly used for 2 purposes:
- Conduct parameter passing by reference such as name.
- Establish pointer values and address of operator’s point to the memory location because the value of the pointer is the memory location or memory address. The data item saved in memory.
Real-time Example
- If the user is trying to locate the name “paramesh” within the data and the string variable named as name and it will look like char[]=”paramesh”. Then the address operator is used to know the location or the address of the data using the “name” variable.
How does Address Operator work in C?
The address operator is working for returns the memory address of a variable. These addresses are returned by the address of the operator are known as pointers because they point to the variable in memory.
Scanning the user input
Code:
scanf("%d",&variable_name); //stores the value of the variable
Displaying the address of the variable
Code:
int a=10;
int address=&a;
Examples
Below are the examples mentioned:
Example #1
Scanning user integer input and display with the ampersand
Code:
//include is used to add basic C libraries
#include <stdio.h>
//main method is used to run C application
int main(void)
{
//declaring 2 variables
int first_number, second_number;
//Asking user to enter integer input
printf("Please enter any 2 integer number \n");
//store the number with ampersand symbol
scanf("%d\n%d",&first_number,&second_number);
//displaying output to the end user
printf("User enter numbers %d and %d ", first_number, second_number);
return 0;
}
Output:
Example #2
Scanning user String input and display with the ampersand
Code:
//include is used to add basic C libraries
#include <stdio.h>
//main method is used to run C application
int main(void)
{
//declaring 2 variables
char first_name[30],last_name[20];
//Asking user to enter input
printf("Please enter your first name = ");
//Store the first_name in ampersand symbol
scanf("%s",&first_name);
//Asking user to enter input
printf("Please enter your last name = ");
//Store the first_name in ampersand symbol
scanf("%s",&last_name);
//displaying output to the end user
printf("Your name is %s %s ", first_name,last_name);
return 0;
}
Output:
Example #3
Address of the String input names
Code:
//include is used to add basic C libraries
#include <stdio.h>
//main method is used to run C application
int main(void)
{
//declaring 2 variables
char first_name[30],last_name[20];
//Asking user to enter input
printf("Please enter your first name = ");
//Store the first_name in ampersand symbol
scanf("%s",&first_name);
//assigning first_name address to a variable
int a=&first_name;
//Asking user to enter input
printf("Please enter your last name = ");
//Store the first_name in ampersand symbol
scanf("%s",&last_name);
//assigning first_name address to a variable
int b=&last_name;
//displaying output to the end user
//As it is address output may vary from compiler to compiler
printf("Address of Your name is %x %x ", a,a);
return 0;
}
Output:
Example #4
Address of the address
Code:
//include is used to add basic C libraries
#include <stdio.h>
//main method is used to run C application
int main(void)
{
//declaring 3 variables
int *x;
int **y;
int input;
//Asking user to enter input
printf("Please enter a number = ");
//Store the first_name in ampersand symbol
scanf("%d",&input);
//take the address of the input into the x single pointer
x=&input;
//take the address of the x into the y double pointer, it will give the address of the address
y=&input;
//displaying output to the end user
//As it is address output may vary from compiler to compiler
printf("Value of input is %d \n",input);
printf("Address of *x is %x\n", &x);
printf("Address of **x is %x", &y);
return 0;
}
Output:
Conclusion
Address operator is used to storing the address of the variable in C. This is denoted by an ampersand (&). This is also used for scanning the user input.
Recommended Articles
This is a guide to Address Operator in C. Here we discuss the introduction to Address Operator in C, why it is used and how does it work with examples. You can also go through our other related articles to learn more –