The Address Operator in C is a special unary operator that returns the address of a variable. It is denoted as the Ampersand Symbol ( & ). This operator returns an integer value which is the address of its operand in the memory. We can use the address operator (&) with any kind of variables, array, strings, functions, and even pointers.
Syntax
The address operator is generally used as a prefix to its operand:
&operand
where operand can be a variable, array, function, pointer, etc.
Examples of Address Operators
Example 1:
Simple C example to demonstrate how to use the address operator in our program.
C
// C program to illustrate the use of address operator
#include <stdio.h>
int main()
{
// declaring a variable
int x = 100;
// printing the address of the variable
printf("The address of x is %p", &x);
return 0;
}
OutputThe address of x is 0x7fffe8f5591c
Explanation
A variable x was defined and initialized with the value 100 in the program above. We retrieved the address of this variable x by using the address operator (&) as the prefix and printed it using printf() function.
Note: The %p format specifier to print the address in hexadecimal form.
Generally, the value returned by the address operator is stored in the pointer variable and then the pointer is dereferenced to get the value stored in that address.
Example 2:
Using a pointer to store the address returned by the address operator and then dereferencing it.
C
// C program to illustrate the use of address operator with
// pointer
#include <stdio.h>
int main()
{
// integer variable
int x = 1;
// integer pointer
int* ptrX;
// pointer initialization with the address of x
ptrX = &x;
// accessing value of x usin pointer
printf("Value of x: %d\n", *ptrX);
return 0;
}
Example 3:
Some standard functions like scanf() also require the address of the variable. In these cases, we use the address operator.
C
// C Program to illustrate the use of address operator with
// scanf()
#include <stdio.h>
int main()
{
// defining variable
int number;
printf("Enter any number: ");
// using adress operator & in scanf() to get the value
// entered by the user in the console
scanf("%d", &number);
// priting the entered number
printf("The entered number is: %d", number);
return 0;
}
Output
Enter any number: 10
The entered number is: 10
Address Operator Incompitable Entities in C
There are some entities in C for which we cannot use the address operator i.e. we cannot get the address of those entities in C. Some of them are:
- Register Variables
- Bit Fields
- Literals
- Expressions
Applications of Address Operator (&):
The address operator (&) is widely used in C programs to get the addresses of different entities. Some of the major and most common applications are:
- Passing Pointers as Function Arguments
- Pointer Arithmetic
- Implementing Data Structures
Similar Reads
Assignment Operators in C++ In C++, the assignment operator forms the backbone of computational processes by performing a simple operation like assigning a value to a variable. It is denoted by equal sign ( = ) and provides one of the most basic operations in any programming language i.e. assign some value to the variables in
6 min read
Bitwise Operators in C In C, bitwise operators are used to perform operations directly on the binary representations of numbers. These operators work by manipulating individual bits (0s and 1s) in a number.The following 6 operators are bitwise operators (also known as bit operators as they work at the bit-level). They are
6 min read
Unary Operators In C++ In C++, unary operators are the type of operators that work on a single value (operand). They perform operations like changing a value's sign, incrementing or decrementing it by one, or obtaining its address.C++ has a total of 9 unary operators:Table of ContentIncrement Operator (++)Decrement Operat
6 min read
&& operator in Java with Examples && is a type of Logical Operator and is read as "AND AND" or "Logical AND". This operator is used to perform "logical AND" operation, i.e. the function similar to AND gate in digital electronics. One thing to keep in mind is the second condition is not evaluated if the first one is false, i.
1 min read
Printing the Address of an Object of Class in C++ Prerequisite: Classes and Objects in C++ The location of an object in the memory is called its address. Addressing is a necessary part of C++, it enables us to use any element as a reference and maintains the uniqueness of all the elements whether it is any variable, object, or container. In this ar
3 min read