C - Pointers

C Programming

Share

In this tutorial we will learn about Pointers in C programming language.

We know that in C if we want to save some value we take help of a variable.

So, a variable stores some value and we access that value by write the variable name.

In the following example we are storing an integer value in a variable num and to access that value we are writing the name of the variable.

// assign value to variable
int num = 10;

// access value stored in the variable
printf("%d", num);

Three things happen when we create a variable and assign a value to it.

  • A memory location is allocated to the variable.
  • The value assigned to the variable is stored in that allocated memory location.
  • The allocated memory location is referred by the name of the variable.


What are Pointers?

Pointers are derived data types and they store memory address.

So, pointers are variables that store memory addresses which are memory location of other variables.

Creating pointer variables

Before we can use a pointer variable we first have to create or declare them like any other variable in C programming.

Syntax of a pointer variable declaration is given below.

dataType *pointerVar;

In the above syntax the * asterisk indicates that we are creating a pointer variable.

The name of the pointer variable in the above syntax is pointerVar.

The created pointer variable pointerVar will point at the variable of type dataType.

Example

In the following example we are creating an integer pointer variable which will point at a variable of type int.

int *ptr;

Similarly, we can create a character pointer variable which will point at a variable of type char.

char *ptr;

Address of operator &

We use the address of & operator to get the address of a variable.

In the following syntax we will get the address of a variable varName.

&varName

Value at the address of operator *

We use the value at the address of * operator to get the value stored at any given address of a variable.

In the following syntax we will get the value stored at the given address.

*varName

Example

In the given example we are printing the address of the variable num and value stored at the address of the variable num.

#include <stdio.h>

int main(void) {
  // creating variable num
  int num;

  // assigning value to num
  num = 10;

  // printing the value stored in variable num
  printf("Value in num: %d\n", num);

  // printing the address of the variable num
  printf("Address of num: %ld\n", &num);
  
  // printing the value stored at the address of the variable num
  printf("Value at the address of num: %d\n", *(&num));
  
  return 0;
}

Note! In the above code &num gives us the address of variable num and *(&num) gives us the value stored at the address of the variable num.

Output:

Value in num: 10
Address of num: 140732733818936
Value at the address of num: 10

Garbage value in pointer variables

By default, when we declare a pointer variable it gets initialized by a random value a.k.a. garbage value.

In the following example we have created a pointer variable of type int. Since we have not assigned any address of any int type variable so, it holds some garbage value.

#include <stdio.h>

int main(void) {
  
  // pointer variable of type int
  int *ptr;
  
  // by default pointer variable ptr
  // holds garbage value as no
  // int variable address was assigned
  printf("Value stored in ptr: %ld\n", ptr);
  
  return 0;
}

Output:

Value stored in ptr: 140732859951192

We generally initialise a pointer variable to NULL value to avoid the random garbage value.

#include <stdio.h>

int main(void) {
  
  // pointer variable of type int
  int *ptr = NULL;
  
  printf("Value stored in ptr: %ld\n", ptr);
  
  return 0;
}

Output:

Value stored in ptr: 0

Initialising pointer variable with address

To initialise pointer variable with the address of another variable we use the & address of operator.

In the following example we have an integer variable num that holds value 10.

We also have a ptr pointer variable that holds the value of integer variable num. So, ptr variable is pointing at the num variable.

#include <stdio.h>

int main(void) {
  
  // num variable
  int num = 10;
  
  // pointer variable
  int *ptr;
  
  // assign address of num variable to ptr pointer variable
  ptr = &num;
  
  // print the value stored in num using the num variable
  printf("Value stored in num: %d\n", num);
  
  // print address of num stored in ptr variable
  printf("Address of num variable: %ld\n", ptr);
  
  // print address of ptr variable
  printf("Address of ptr variable: %ld\n", &ptr);
  
  // print the value stored in num using ptr variable
  printf("Value stored in num using ptr variable: %d\n", *ptr);
  
  return 0;
}

Output:

Value stored in num: 10
Address of num variable: 140732844468280
Address of ptr variable: 140732844468272
Value stored in num using ptr variable: 10


In the above image we are showing only the last 4 digits of the address.

Share

Recently Updated