MCS-011 Problem Solving and Programming

First year, Semester 1

Chapters

Newsletter

Address and Indirection Operators

In C programming, two important operators are used with pointers: the address operator (&) and the indirection operator (*).

Address Operator (&):

  • It is used to find the memory address where a variable is stored.
  • When you use & before a variable, it gives you the location in memory where that variable is kept.
  • Example: If you have an integer variable x, using &x will give you the address of x.

Indirection Operator (*):

  • It is used to access the value stored at a specific memory address.
  • When you use * with a pointer (a variable that holds a memory address), it retrieves the value stored at that address.
  • Example: If you have a pointer p that holds the address of x, then *p will give you the value of x.

Example

#include <stdio.h>
int main() { int x = 10; // Declare an integer variable x int *ptr = &x; // Declare a pointer variable ptr and assign it the address of x // Print the address of x using the address operator & printf("Address of x: %p\n", (void*)&x); // Print the value stored at the address held by ptr using the indirection operator * printf("Value at address held by ptr: %d\n", *ptr); return 0; }

Report an issue

Reporting: Address and Indirection Operators (topic)

Related Posts