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 = #
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 = #
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 = # // 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 = # // 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.