Open In App

Address Operator & in C

Last Updated : 16 Oct, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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.

Address-Operator-in-C

Address Operator in C

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;
}


Output

The 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;
}


Output

Value of x: 1


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:

  1. Register Variables
  2. Bit Fields
  3. Literals
  4. 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:

  1. Passing Pointers as Function Arguments
  2. Pointer Arithmetic
  3. Implementing Data Structures


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads