C Pointers

A pointer is a variable that stores the memory address of another variable as its value.


A pointer variable points to a data type (like int) of the same type and is created with the * operator. The address of the variable you're working with is assigned to the pointer:


Code:


  #include 

  int main()
  {
  int myAge = 43;
  int* ptr = &myAge; // A pointer variable, with the name ptr, that stores the address of myAge

  // Output the value of myAge
  printf("%d\n", myAge);

  // Output the memory address
  printf("%p\n", &myAge);

  // Output the memory address
  printf("%p\n", ptr);

  return 0;
  }


Output:

43

0x7ff5367e044

0x7ff5367e044

Explanation:

We are creating an int variable that points to a pointer variable called ptr. Keep in mind that the pointer's type must match with the type of the variable you are manipulating.


Use the & operator to store the memory address of the myAge variable, and assign it to the pointer.


Now, ptr holds the value of myAge's memory address.