In C, pointers are used to store and manipulate the addresses of variables. Two important operators, the dereference (*) and address-of (&), play crucial roles in working with pointers effectively. Let's explore each in detail.

Address-of Operator (&)

The address-of operator (&) is used to obtain the memory address of a variable. This is essential when you want to assign the address to a pointer.

Syntax and Example

The syntax for using the address-of operator is:

pointer_name = &variable_name;
  • &variable_name gives the memory address of variable_name.

Example

#include <stdio.h> int main() { int a = 10; int *p = &a; // 'p' holds the address of 'a' printf("Address of a: %p\n", (void*)&a); // Prints the memory address of 'a' printf("Address stored in pointer p: %p\n", (void*)p); // Prints the same address return 0; }

Output:

Address of a: 0x7ffeeb3d2c9c (example output) Address stored in pointer p: 0x7ffeeb3d2c9c

In this example:

  • &a gives the memory address of variable a.
  • p stores that address, so p "points to" a.

Dereference Operator (*)

The dereference operator (*) is used to access or modify the value stored at the memory address that a pointer holds. It effectively "follows" the pointer to the location in memory and allows you to interact with the data stored there.

Syntax and Example

The syntax for dereferencing a pointer is:

*pointer_name;
  • *pointer_name gives the value at the memory address stored in pointer_name.

Example

#include <stdio.h> int main() { int a = 10; int *p = &a; // 'p' holds the address of 'a' printf("Value of a: %d\n", a); // Output: 10 printf("Value through pointer p: %d\n", *p); // Output: 10 *p = 20; // Change the value of 'a' through the pointer printf("New value of a: %d\n", a); // Output: 20 return 0; }

Output:

Value of a: 10 Value through pointer p: 10 New value of a: 20

In this example:

  • *p gives the value stored at the memory address p holds, which is the value of a.
  • *p = 20 modifies the value of a indirectly through the pointer.

How They Work Together

The address-of (&) and dereference (*) operators work together to enable powerful pointer operations:

  • &: Gets the address of a variable.
  • *: Gets the value stored at the address held by the pointer.

Example of Using Both Together

#include <stdio.h> int main() { int x = 5; int *ptr = &x; // 'ptr' points to 'x' printf("Value of x: %d\n", x); // Output: 5 printf("Address of x: %p\n", (void*)&x); // Address of 'x' printf("Address stored in ptr: %p\n", (void*)ptr); // Same address as '&x' printf("Value at address ptr points to: %d\n", *ptr); // Output: 5 (value of 'x') return 0; }
  • ptr = &x: ptr is assigned the address of x.
  • *ptr: This dereferences ptr to get the value of x.

Summary

  • Address-of Operator (&):

    • Used to obtain the memory address of a variable.
    • Essential for initializing pointers.
    • Example: int *ptr = &variable; assigns the address of variable to ptr.
  • Dereference Operator (*):

    • Used to access or modify the value at the memory address stored in the pointer.
    • Allows indirect interaction with the variable.
    • Example: *ptr = 20; changes the value of the variable pointed to by ptr.

Pointers, along with the address-of and dereference operators, provide a way to directly access and manipulate memory, which is powerful for managing data and optimizing performance, especially in systems programming and memory-constrained environments.