Introduction
The address-of operator (&) is a unary operator that returns the memory address of an object. It is one of the most fundamental operators in C because it allows pointers to reference variables stored in memory.
Without the address-of operator, pointers could not be initialized with the addresses of variables, making many features of C—such as pass-by-reference, dynamic memory management, and data structures—impossible.
Understanding how the address-of operator works is essential before learning pointers, arrays, functions, and memory management.
What Is the Address-of Operator?
The address-of operator (&) returns the memory address of its operand.
Syntax
&object
The result of the expression is a pointer to the specified object.
Basic Example
The following program uses the address-of operator to obtain the address of an integer variable.
Example
#include <stdio.h>
int main(void)
{
int number = 100;
printf("%p\n", (void *)&number);
return 0;
}
Output
0x7ffcb0b1f8dc
Using the Address-of Operator to Initialize a Pointer
The most common use of the address-of operator is initializing a pointer with the address of an object.
Example
int number = 100;
int *ptr = &number;
Here:
numberstores the integer value.&numberproduces its memory address.ptrstores that address.
Relationship Between & and *
The address-of operator (&) and the dereference operator (*) are complementary.
| Operator | Purpose |
|---|---|
& |
Obtains the address of an object. |
* |
Accesses the object stored at an address. |
Example:
Example
int value = 50;
int *ptr = &value;
printf("%d\n", *ptr);
In this example:
&valuereturns the address ofvalue.*ptraccesses the value stored at that address.
Using the Address-of Operator with Different Data Types
The address-of operator can be applied to most objects regardless of their type.
| Object | Result Type |
|---|---|
int |
int * |
char |
char * |
float |
float * |
double |
double * |
struct |
Pointer to the structure type. |
Example with Multiple Variables
Example
#include <stdio.h>
int main(void)
{
int age = 25;
float height = 1.75f;
char grade = 'A';
printf("%p\n", (void *)&age);
printf("%p\n", (void *)&height);
printf("%p\n", (void *)&grade);
return 0;
}
Each variable occupies its own location in memory, so each address is different.
Passing Variables by Reference
The address-of operator is commonly used when calling functions that need to modify the original variable.
Example
#include <stdio.h>
void increment(int *value)
{
(*value)++;
}
int main(void)
{
int number = 5;
increment(&number);
printf("%d\n", number);
return 0;
}
Output
6
The function receives the address of number, allowing it to modify the original variable.
Using & with Arrays
The address-of operator can also be applied to arrays.
Example
int numbers[5];
printf("%p\n", (void *)numbers);
printf("%p\n", (void *)&numbers);
printf("%p\n", (void *)&numbers[0]);
Although these expressions often print the same address, they have different types:
| Expression | Type |
|---|---|
numbers |
int * |
&numbers[0] |
int * |
&numbers |
int (*)[5] |
numbers and &numbers[0] usually evaluate to the same address, they are conceptually different expressions.
Using & with Structures
The address-of operator is frequently used with structures.
Example
struct Student
{
int id;
};
struct Student student;
struct Student *ptr = &student;
The pointer ptr now stores the address of the structure object.
Objects That Cannot Use the Address-of Operator
The address-of operator requires an object with a storage location. It cannot be applied to every expression.
Invalid examples include:
Example
&(10);
&(a + b);
Integer constants and temporary expression results do not represent objects with accessible storage that can be addressed.
& to an invalid expression results in a compilation error.
Common Beginner Mistakes
- Confusing the address-of operator (
&) with the bitwise AND operator. - Using
&when the function expects a value instead of a pointer. - Forgetting to use
&when a function expects a pointer parameter. - Attempting to obtain the address of an invalid expression.
- Printing addresses with the wrong format specifier instead of
%p.
Best Practices
- Use
&only with valid objects. - Print addresses using the
%pformat specifier. - Cast pointer arguments to
(void *)when printing with%p. - Understand the relationship between
&and*. - Always match pointer types with the addresses they store.
&variable, think of it as "the address where variable is stored," not the value contained in the variable itself.
Summary
The address-of operator (&) returns the memory address of an object. It is the primary tool for initializing pointers and passing variables by reference.
| Expression | Description |
|---|---|
&value |
Address of an object. |
int *ptr = &value; |
Initialize a pointer. |
function(&value); |
Pass an object's address to a function. |
(void *)&value |
Print an address using %p. |
The address-of operator is one of the building blocks of pointer programming in C. Once you understand how to obtain and use memory addresses, you are ready to explore pointer dereferencing, pointer arithmetic, arrays, structures, and dynamic memory management.