C Programming

⌘K
  1. Home
  2. Docs
  3. C Programming
  4. Pointers
  5. The & and * operator

The & and * operator

The & operator is used to get the address of a variable in memory.

Thank you for reading this post, don't forget to subscribe!
  • It returns the memory location of the variable.
  • It is mainly used to assign the address to a pointer.

Syntax:

&variable_name

Example:

#include <stdio.h>

int main() {
    int a = 5;
    printf("Value of a: %d\n", a);
    printf("Address of a: %p\n", &a);  // &a gives the memory address of variable a
    return 0;
}

The * operator is used to access the value at a given address. It is also used to declare a pointer.

Syntax:

*pointer_name

Example:

#include <stdio.h>

int main() {
    int a = 10;
    int *ptr = &a;  // ptr stores address of a

    printf("Value of a: %d\n", a);
    printf("Address of a: %p\n", ptr);        // Using pointer
    printf("Value at ptr: %d\n", *ptr);       // Dereferencing the pointer

    return 0;
}

How can we help?