Table of contents
1.
Introduction
2.
Syntax
3.
Examples of Address Operators
3.1.
Example 1: Storing the address of a variable in a pointer
3.2.
Example 2: Modifying the value of a variable through a pointer
4.
Address Operator Incompatible Entities in C
5.
Applications of Address Operator (&)
6.
Frequently Asked Questions 
6.1.
Can the address operator be used with constants?
6.2.
Is it possible to use the address operator with expressions?
6.3.
Can the address operator be used with register variables?
7.
Conclusion
Last Updated: Dec 4, 2024
Easy

Address Operator & in C

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

The address operator (&) is a very important concept in the C language. It is used to obtain the memory address of a variable or an expression. By using the address operator, you can access and manipulate data stored in specific memory locations. This operator is essential for working with pointers, which are variables that store memory addresses. 

Address Operator & in C

In this article, we will discuss the syntax of the address operator with examples, discuss its incompatible entities, and look into its applications.

Syntax

In C, the address operator is denoted by the ampersand symbol (&). To use the address operator, you simply place it before a variable or an expression. The syntax is :

&variable_name


or

&(expression)


When you apply the address operator to a variable or an expression, it returns the memory address where that variable or expression is stored. The resulting address is typically represented as a hexadecimal number.


For example: 

#include <stdio.h>

int main() {
    int num = 10;
    printf("Value of num: %d\n", num);
    printf("Address of num: %p\n", &num);
    return 0;
}
You can also try this code with Online C Compiler
Run Code

 

Output

Value of num: 10
Address of num: 0x7ffcdd30e654


In this code, we declare an integer variable `num` and assign it the value 10. We then use `printf` to display both the value of `num` and its memory address using the address operator `&num`. The `%p` format specifier is used to print the memory address in hexadecimal format.

Examples of Address Operators

Let's look at a few more examples to understand the use of the address operator in C.

Example 1: Storing the address of a variable in a pointer

#include <stdio.h>
int main() {
    int num = 20;
    int *ptr = &num;
    printf("Value of num: %d\n", num);
    printf("Address of num: %p\n", &num);
    printf("Value of ptr: %p\n", ptr);
    return 0;
}
You can also try this code with Online C Compiler
Run Code

 

Output

Value of num: 20
Address of num: 0x7ffdf1ca9c44
Value of ptr: 0x7ffdf1ca9c44


In this example, we declare an integer variable `num` and a pointer variable `ptr`. We assign the address of `num` to `ptr` using the address operator `&num`. We then print the value of `num`, the address of `num` using `&num`, and the value of `ptr`, which holds the same address as `&num`.

Example 2: Modifying the value of a variable through a pointer

#include <stdio.h>
int main() {
    int num = 20;
    int *ptr = &num;
    printf("Before modification: %d\n", num);
    *ptr = 30;
    printf("After modification: %d\n", num);
    return 0;
}
You can also try this code with Online C Compiler
Run Code

 

Output

Before modification: 20
After modification: 30

 

Here, we demonstrate how we can use a pointer to modify the value of a variable indirectly. We assign the address of `num` to `ptr` using the address operator. Then, by dereferencing `ptr` using the `*` operator and assigning a new value of 30, we modify the value of `num`. The output will show the value of `num` before and after the modification.

Address Operator Incompatible Entities in C

It's important to note that the address operator (&) cannot be applied to all types of entities in C. There are certain limitations and incompatibilities to keep in mind.

1. Constants: The address operator cannot be used with constants. For example, the following code will result in a compilation error:

const int num = 10;
int *ptr = &num;  // Error: cannot take the address of a constant


2. Expressions: The address operator cannot be directly applied to expressions that do not lvalue. An lvalue is an expression that represents a memory location and can appear on the left side of an assignment operator. For instance:

int *ptr = &(5 + 3);  // Error: cannot take the address of an expression


3. Register variables: Variables declared with the `register` keyword are stored in CPU registers rather than memory. The address operator cannot be used with register variables. For example:

register int num = 10;
int *ptr = &num;  // Error: cannot take the address of a register variable


4. Bitfields: Bitfields are used to specify the number of bits for a member in a structure. The address operator cannot be applied to bitfields. For example:

struct bitfield {
    unsigned int a : 3;
};
struct bitfield bf;
int *ptr = &(bf.a);  // Error: cannot take the address of a bitfield

Applications of Address Operator (&)

1. Pointers: The address operator is primarily used to initialize and work with pointers. By obtaining the address of a variable using the & operator, you can assign it to a pointer variable. This allows you to indirectly access and manipulate the value stored at that memory location.
 

2. Function parameters: When you want to modify the value of a variable inside a function and have the changes reflect in the calling function, you can pass the address of the variable as a parameter. This is known as pass-by-reference. By using the address operator, you can pass the memory address of a variable to a function. 
 

3. Arrays: The address operator is often used with arrays to obtain the memory address of array elements. In C, the name of an array itself represents the address of the first element. By using the address operator with array indexing, you can access the addresses of individual elements. 
 

4. Dynamic memory allocation: The address operator is used in conjunction with dynamic memory allocation functions like `malloc()` and `calloc()`. These functions return the address of the allocated memory block, which can be stored in a pointer variable. 

Frequently Asked Questions 

Can the address operator be used with constants?

No, the address operator cannot be used with constants because they do not have a modifiable memory location.

Is it possible to use the address operator with expressions?

The address operator cannot be directly applied to expressions that do not have an lvalue, meaning they do not represent a memory location.

Can the address operator be used with register variables?

No, the address operator cannot be used with variables declared as "register" because they are stored in CPU registers rather than memory.

Conclusion

In this article, we have learned about the address operator (&) in C, its syntax, and how it is used to obtain the memory address of variables and expressions. We looked at examples showing its use, discussed incompatible entities, and highlighted its applications in pointers, function parameters, arrays, and dynamic memory allocation. 

You can also check out our other blogs on Code360.

Live masterclass