Utilizor
Contact Us
HomeC TutorialC Pointers

CC Pointers

Memory addresses.

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 are working with is assigned to the pointer:

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

Dereference

You can also use the pointer to get the value of the variable, by using the * operator (the dereference operator).

Examples

Create Pointer

c example

Printing address and value.

#include <stdio.h>

int main() {
  int myAge = 43;
  int* ptr = &myAge;

  printf("%d\n", myAge);  // Outputs the value of myAge (43)
  printf("%p\n", &myAge); // Outputs the memory address of myAge
  printf("%p\n", ptr);    // Outputs the memory address of myAge with the pointer
  return 0;
}

Dereference

c example

Getting value from address.

#include <stdio.h>

int main() {
  int myAge = 43;
  int* ptr = &myAge;

  printf("%p\n", ptr);
  printf("%d\n", *ptr); // Dereference: outputs the value (43)
  return 0;
}

Test Your Knowledge

1. Which operator is used to create a pointer variable?

2. Which operator is used to get the memory address of a variable?