Back to C Programming
2025-11-287 min read

Address-of operator (C Programming)

Learn Address-of operator (C Programming) step by step with clear examples and exercises.

Why This Matters

In C programming, understanding and mastering the Address-of operator (&) is crucial for working with pointers effectively. Pointers are essential for advanced memory management, dynamic memory allocation, function arguments, and more complex programming techniques. By learning how to use this operator, you will be better equipped to tackle real-world coding challenges and solve problems efficiently.

Prerequisites

Before diving into the Address-of operator, it's essential to have a solid understanding of the following topics:

  1. Basic C syntax (variables, data types, operators)
  2. Arrays and multi-dimensional arrays
  3. Pointers introduction (dereferencing with *)
  4. Input/output functions (scanf(), printf())
  5. Understanding structures and arrays of structures
  6. Basic file I/O operations (fopen(), fread(), fwrite(), fclose())
  7. Understanding the concept of memory allocation in C
  8. Basic understanding of data structures like linked lists, stacks, and queues

Core Concept

The Address-of operator (&) is a unary operator that returns the memory address of its operand. The operand can be a variable, an array element, a structure member, or a function. In this section, we will explore how to use it with different types of operands and provide examples for each case.

Variables

To get the memory address of a variable, simply place the & operator before the variable name. For example:

int num = 10;
int *ptr = # // ptr now stores the memory address of num

In this case, we created an integer variable num and a pointer ptr. We used the Address-of operator to store the memory address of num in ptr.

Arrays

When using arrays with the Address-of operator, you can get the memory address of the entire array or a specific element. For example:

int arr[5] = {1, 2, 3, 4, 5};
int *ptr = &arr; // ptr stores the memory address of the first element in arr (arr[0])

In this case, we created an array arr and a pointer ptr. We used the Address-of operator to store the memory address of the entire array (first element) in ptr.

Structures

To get the memory address of a structure member, you can use the Address-of operator with the dot notation. For example:

typedef struct {
int id;
char name[20];
} Person;

Person person = {1, "John Doe"};
int *ptr = &person.id; // ptr stores the memory address of person's id

In this case, we created a structure Person, an instance person, and a pointer ptr. We used the Address-of operator to store the memory address of person's id in ptr.

Functions

To get the memory address of a function, simply place the & operator before the function name. For example:

void myFunction() {
printf("Hello, World!\n");
}

int main() {
void (*ptr)() = &myFunction; // ptr stores the memory address of myFunction
(*ptr)(); // call the function through the pointer
}

In this case, we created a function myFunction() and a pointer ptr. We used the Address-of operator to store the memory address of myFunction in ptr. Then, we called the function using the dereference operator (*) through the pointer.

Worked Example

Let's work on an example that demonstrates the Address-of operator with variables, arrays, and structures.

#include <stdio.h>

typedef struct {
int id;
char name[20];
} Person;

int main() {
int num = 10;
int arr[5] = {1, 2, 3, 4, 5};
Person person = {1, "John Doe"};

// Addresses of variables
printf("Address of num: %p\n", &num);
printf("Address of arr: %p\n", &arr);
printf("Address of person.id: %p\n", &person.id);
printf("Address of person.name: %p\n", &person.name);

// Addresses of array elements
int *ptr1 = &arr[0];
int *ptr2 = &arr[4];
printf("Address of arr[0]: %p\n", ptr1);
printf("Address of arr[4]: %p\n", ptr2);

return 0;
}

In this example, we created an integer variable num, an array arr, a structure Person, and an instance person. We printed the memory addresses of each using the Address-of operator. Additionally, we demonstrated getting the memory addresses of specific array elements by creating pointers to them. Compile and run this code to see the output.

Common Mistakes

  1. Forgetting to include the & operator when creating a pointer:
int num = 10;
int *ptr = num; // This is incorrect! Use ptr = &num instead.
  1. Incorrectly accessing memory using pointers:
int arr[5] = {1, 2, 3, 4, 5};
int *ptr = &arr; // ptr stores the memory address of the first element in arr (arr[0])
ptr++; // Move to the next memory location (which is not an array element)
*ptr = 100; // Write the value 100 at a random memory location, causing undefined behavior
  1. Using the Address-of operator with non-lvalue expressions:
int num = 10;
int *ptr = &num++; // This is incorrect! The expression `num++` has no memory address.
  1. Assigning different data types to a single pointer without type casting:
int num = 10;
float *ptr = &num; // This is incorrect! Use ptr = (float*)&num for type casting.
  1. Using the Address-of operator with bitfields:

Bitfields do not have a specific memory address, so you cannot use the Address-of operator directly with them. However, you can create an array of structs containing the bitfield and use the Address-of operator on the struct.

Practice Questions

  1. Write a program that creates two pointers to integers and assigns them the addresses of two different variables. Then, print the values stored in the original variables using their respective pointers.
  2. Create an array of 10 integers and use the Address-of operator to store the memory addresses of the first and last elements in separate pointers. Write a program that accesses and prints the values of all array elements using these pointers.
  3. Write a function swap() that takes two integer pointers as arguments, swaps their values, and returns nothing. Use this function to swap the values of two variables without creating temporary variables.
  4. Write a program that creates a dynamic memory allocation for an array of 10 integers using the Address-of operator and initializes it with user input. Then, print the sum of all elements in the array.
  5. Create a structure Student containing fields id, name, and age. Write a program that creates an array of 5 Student structures, reads data from the user for each structure, and prints the memory addresses of each structure using the Address-of operator.
  6. Write a function findMin() that takes an integer pointer and an integer as arguments, finds the minimum value in the array pointed to by the pointer, and returns the minimum value. Use this function to find the minimum value in an array without creating temporary variables.
  7. Write a program that creates a linked list of integers using pointers and the Address-of operator. The program should read user input for the number of nodes and their values, dynamically allocate memory for each node, and print the values in reverse order.
  8. Implement a stack data structure using arrays and pointers with the Address-of operator. The program should allow pushing and popping integers onto/from the stack, and print the stack contents.
  9. Write a program that simulates a simple text editor using the Address-of operator and pointers. The program should read user input for lines of text, dynamically allocate memory for each line, and allow users to insert, delete, and display lines.
  10. Implement a queue data structure using arrays and pointers with the Address-of operator. The program should allow enqueuing and dequeuing integers onto/from the queue, and print the queue contents.

FAQ

What is the difference between the Address-of operator (&) and the dereference operator (*)?

  • The Address-of operator (&) gets the memory address of its operand, while the dereference operator (*) accesses the value stored at a given memory address.

Can I use the Address-of operator with function pointers?

  • Yes! You can get the memory address of a function using the Address-of operator and store it in a function pointer variable.

Is it safe to assign different data types to a single pointer using the Address-of operator?

  • No, it's not safe to do so because pointers are type-specific. Assigning a different data type will result in undefined behavior. However, you can use type casting (e.g., (float*)) to achieve this with caution.

Can I use the Address-of operator with bitfields?

  • No, you cannot use the Address-of operator with bitfields directly because they don't have a specific memory address. However, you can create an array of structs containing the bitfield and use the Address-of operator on the struct.

How can I check if two pointers point to the same memory location?

  • You can compare the addresses stored in the pointers using the equality operator (==). If both pointers have the same address, they point to the same memory location.