How to create and use a pointer in C

About C Pointer

In C programming, a pointer is a variable that stores the memory address of another variable.

Instead of holding a direct value, a pointer holds the address of a memory location where data is stored.

It is used to get the memory address of a variable.

It is also used to get the value at the memory location pointed to by a pointer.

It is used for more flexible and dynamic memory management.

Pointer Declaration & Initialization

To declare a pointer, an asterisk '*' also known as the dereference operator is used before the variable name (example: *pointer_Variable).

To get the memory address of a variable, the address-of operator (&) is used before the variable name (example: variable name number and memory address &number). Here is an example of declaring & initializing a pointer:

Explanation of code:

  • int number = 30;: This line declares an integer variable named 'number' and initializes the variable with value '30'.
  • int *pointer_Variable = &number;: This line declares a pointer variable named 'pointer_Variable' (an asterisk '*' is used before the variable name) and initializes the variable with the memory address of 'number' variable by adding '&' (address-of operator) before the variable name (&number).

Get the Memory Address of a Variable Using a Pointer

The pointer variable hold the memory address of a variable as demonstrated in the following example:

Example of getting memory address of a variable using a pointer
Output of the above example
Address of number variable using pointer: 0028FF28

Please note that the memory address of a variable can vary in different pcs.

Explanation of code:

  • int number = 30;: This line declares an integer variable named 'number' and initializes the variable with value '30'.
  • int *pointer_Variable = &number;: This line declares a pointer variable named 'pointer_Variable' and initializes the variable with the memory address of 'number' variable &number. To get the memory address '&' is added with the 'number' variable.
  • printf("Address of number variable using pointer: %p\n", pointer_Variable);: This line prints the address stored in 'pointer_Variable'. The format specifier '%p' is used to print the address stored in the pointer. '\n' creates a newline in C.

Get the Value of a Variable Using a Pointer

By using the dereference operator '*' before pointer variable, you can access the value stored at the address the pointer is pointing to as demonstrated in the following example:

Example of getting value of a variable using a pointer
Output of the above example
The value of number variable using pointer: 30

Explanation of code:

  • int number = 30;: This line declares an integer variable named 'number' and initializes the variable with value '30'.
  • int *pointer_Variable = &number;: This line declares a pointer variable named 'pointer_Variable' and initializes the variable with the address of 'number' variable &number.
  • printf("The value of number variable using pointer: %d\n", *pointer_Variable);: This line prints the value stored at the address the pointer is pointing to. The format specifier '%d' is used to print the integer data the pointer is pointing to.

Change the Value of a Variable Using a Pointer

You can change the value stored at the address the pointer is pointing to by assigning new value to the pointer as demonstrated in the following example:

Example of changing value of a variable using a pointer
Output of the above example
The new value of number variable using pointer: 50

Explanation of code:

  • int number = 30;: This line declares an integer variable named 'number' and initializes the variable with value '30'.
  • int *pointer_Variable = &number;: This line declares a pointer variable named 'pointer_Variable' and initializes the variable with the address of 'number' variable &number.
  • *pointer_Variable = 50;: This line updates the variable value with new value '50' using pointer. Please note that the dereference operator '*' before pointer variable is used here.
  • printf("The value of number variable using pointer: %d\n", *pointer_Variable);: This line prints the new value stored at the address the pointer is pointing to. The format specifier '%d' is used to print the integer data the pointer is pointing to.

Live Code Playground

In the following C code editor, you can practice by creating more pointers. Click the 'Execute Code' button to execute the code; the executed output will be displayed in the following C Code Output frame. Practice until you become comfortable and proficient with your code.

C logo C Online Editor
C Code Output