Address Operator in C (&) - Complete Guide with Examples
Learn how to use the address-of operator to work with memory addresses, pointers, and references in C programming
About the Author
Sankalan Data Technologies Team
Programming and Data Technology Tutorials
This tutorial was created by the Sankalan Data Technologies team. Our instructors have experience teaching C programming, SQL, Python and other programming technologies to students and professionals. Content is written and reviewed by experienced trainers and developers.
Quick Answer: Address Operator in C
The address operator (&) in C returns the memory address of a variable. Here's a simple example:
#include <stdio.h>
int main() {
int x = 10;
printf("Value of x: %d\n", x);
printf("Address of x: %p\n", &x);
return 0;
}
Output:
Value of x: 10
Address of x: 0x7ffc12345678
Note: The actual address value will vary each time you run the program.
What You'll Learn in This Tutorial
- What is the Address Operator?
- Syntax and Usage
- Complete Program Examples
- Common Uses
- Practice Questions
In C programming, understanding memory management is crucial. The address operator (&) is one of the fundamental tools that gives you direct access to memory locations. It's the key to working with pointers, passing variables by reference, and dynamic memory allocation.
Whether you're just starting with C or preparing for a technical interview, mastering the address operator is essential for writing efficient and powerful C programs.
What is the Address Operator (&) in C?
The address operator (&) is a unary operator in C that returns the memory address of its operand. It is also called the reference operator or address-of operator.
Key Points About the Address Operator
- Syntax:
&variable_name - Returns: The memory address where the variable is stored
- Format Specifier: Use
%pto print addresses - Opposite: The dereference operator (*) accesses the value at an address
- Use Cases: Pointers, function arguments (pass by reference), dynamic memory
When you declare a variable in C, the compiler allocates memory for it. The address operator allows you to find out exactly where in memory that variable is stored. This is essential for pointer operations and many advanced C programming concepts.
Syntax and Usage of the Address Operator
The syntax of the address operator is simple: place the & symbol immediately before a variable name.
Basic Syntax
int x = 10;
int *ptr = &x; // Store address of x in pointer variable ptr
Printing Memory Addresses
Program to Print Variable Addresses
#include <stdio.h>
int main() {
int num = 42;
float price = 99.99;
char grade = 'A';
printf("Integer variable:\n");
printf(" Value: %d\n", num);
printf(" Address: %p\n\n", &num);
printf("Float variable:\n");
printf(" Value: %.2f\n", price);
printf(" Address: %p\n\n", &price);
printf("Character variable:\n");
printf(" Value: %c\n", grade);
printf(" Address: %p\n", &grade);
return 0;
}
Output
Integer variable:
Value: 42
Address: 0x7ffc12345678
Float variable:
Value: 99.99
Address: 0x7ffc12345674
Character variable:
Value: A
Address: 0x7ffc12345673
Understanding the Output
- Addresses are in hexadecimal format - They start with
0x - Different variable types have different addresses - Memory is allocated separately
- Addresses change each time you run the program - The OS allocates memory dynamically
- Addresses are contiguous - Variables allocated consecutively have nearby addresses
Complete Program Examples
Example 1: Working with Pointers and Addresses
#include <stdio.h>
int main() {
int value = 25;
int *ptr = &value;
printf("Direct access:\n");
printf(" Value: %d\n", value);
printf(" Address: %p\n\n", &value);
printf("Pointer access:\n");
printf(" Pointer value (address): %p\n", ptr);
printf(" Value at address: %d\n", *ptr);
// Changing value through pointer
*ptr = 50;
printf("\nAfter changing through pointer:\n");
printf(" New value: %d\n", value);
return 0;
}
Output
Direct access:
Value: 25
Address: 0x7ffc12345678
Pointer access:
Pointer value (address): 0x7ffc12345678
Value at address: 25
After changing through pointer:
New value: 50
Example 2: Address Operator with Arrays
#include <stdio.h>
int main() {
int numbers[] = {10, 20, 30, 40, 50};
int i;
printf("Array elements and their addresses:\n");
for (i = 0; i < 5; i++) {
printf("numbers[%d] = %d at address %p\n",
i, numbers[i], &numbers[i]);
}
// Array name acts as a pointer to first element
printf("\nArray name as pointer:\n");
printf("numbers (first element address): %p\n", numbers);
printf("&numbers[0] (first element address): %p\n", &numbers[0]);
return 0;
}
Output
Array elements and their addresses:
numbers[0] = 10 at address 0x7ffc12345678
numbers[1] = 20 at address 0x7ffc1234567c
numbers[2] = 30 at address 0x7ffc12345680
numbers[3] = 40 at address 0x7ffc12345684
numbers[4] = 50 at address 0x7ffc12345688
Array name as pointer:
numbers (first element address): 0x7ffc12345678
&numbers[0] (first element address): 0x7ffc12345678
Example 3: Passing Variables by Reference
#include <stdio.h>
// Function to swap two values using pointers
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
int main() {
int x = 10, y = 20;
printf("Before swap:\n");
printf(" x = %d, y = %d\n", x, y);
printf(" Address of x: %p\n", &x);
printf(" Address of y: %p\n\n", &y);
// Pass addresses to swap function
swap(&x, &y);
printf("After swap:\n");
printf(" x = %d, y = %d\n", x, y);
return 0;
}
Output
Before swap:
x = 10, y = 20
Address of x: 0x7ffc12345678
Address of y: 0x7ffc12345674
After swap:
x = 20, y = 10
Common Uses of the Address Operator
- Pointers - Storing addresses in pointer variables for indirect access
- Pass by Reference - Modifying variables inside functions
- Dynamic Memory - Working with malloc, calloc, and free
- Array Access - Understanding array-to-pointer decay
- Debugging - Printing memory addresses for debugging
- Data Structures - Building linked lists, trees, and graphs
Important: The address operator can only be applied to
lvalues (variables that have a memory location). You cannot use
it on literals or expressions: &10 or &(x + y) is invalid.
Common Mistakes to Avoid
- Using & with constants -
&10is invalid syntax - Forgetting %p format specifier - Using %d with addresses causes warnings
- Modifying addresses - Addresses are read-only; you can't change where a variable is stored
- Uninitialized pointers - Using pointers without assigning addresses first
- Confusing & and * - & gets address, * gets value at address
- Address of register variables - You cannot get the address of register variables
Practice Questions
Try these exercises to reinforce your understanding:
- Write a program that declares three variables of different types and prints their values and addresses.
- Write a program that uses a pointer to modify the value of a variable from another part of the program.
- Write a function that takes an integer pointer and doubles the value it points to.
- Write a program that prints all the elements of an array along with their memory addresses.
- Write a swap function that swaps two integers using pointers (like the example above).
- Write a program that stores user input in a variable and prints its memory address.
Hint: Use the & operator to get addresses and the * operator to access values at addresses.
Why Understanding the Address Operator Matters
- Foundation Concept: The address operator is the gateway to understanding pointers, which are fundamental to C programming.
- Interview Relevance: Questions about the address operator and pointers are common in technical interviews.
- Real-World Use: Understanding memory addresses is crucial for systems programming, embedded systems, and performance optimization.
- Advanced Topics: Master this to move on to dynamic memory allocation, data structures, and system-level programming.
Frequently Asked Questions
What is the address operator (&) in C?
The address operator (&) in C is a unary operator that returns the memory address of a variable. It is used to get the location where a variable is stored in memory.
How do I use the address operator in C?
To use the address operator, place it before a variable name: &variable_name. This returns the memory address of that variable. For example, int x = 10; printf("%p", &x); prints the memory address of x.
What is the difference between & and * in C?
& is the address operator that returns the memory address of a variable. * is the dereference operator that accesses the value stored at a memory address. They are opposites: & gets the address, * gets the value at that address.
How do I print memory addresses in C?
Use the %p format specifier with printf: printf("%p", &variable); This prints the memory address in hexadecimal format. For example: printf("Address of x: %p", &x);
What is the format specifier for printing addresses in C?
The correct format specifier for printing memory addresses in C is %p. It prints the address in a platform-specific format, typically hexadecimal. Always use %p with pointers or address values.
Why do we use the address operator in C programming?
The address operator is essential for working with pointers, passing variables by reference to functions, and dynamic memory allocation. It allows you to access and manipulate memory addresses directly.
Related Tutorials
Previous Topic: Types of Pointers in C | Next Topic: Void Pointer in C