C Pointer introduction | What is a Pointer in C

A pointer in C language is a variable that holds the address of another variable of same data type. A pointer is a derived data type and it is built from one of the fundamental data type available in C. Pointers contains memory addresses as their values. For example :


At the diagram, the pointer variable ptr holds the memory address of variable num.

Since these memory addresses are the locations in the computer where program instructions and data are stored, pointers can be used to access and manipulate data stored in the memory. The correct understanding and use of pointers is critical to successful C programming. Some of the main reasons two use pointers are :

  • Pointers provide the means by which functions can modify their calling arguments.
  • Pointers support dynamic memory allocation.
  • Pointers can improve the efficiency of certain routines.
  • Pointers are more efficient in handling Arrays and Structures.

Although C pointer may be little confusing and difficult to understand for beginner, but they are a powerful tool and handy to use one they are mastered.

Symbols used in C Pointer :

There are two symbols used by the pointer variable :

Symbol Name Description
& (ampersand sign) ' address of ' operator Returns the address of a variable
* (asterisk sign) ' at address ' operator Returns the value at the address

1. '&' address of operator :

It is a unary operator that returns the memory address of its operand. Syntax
&variable_name
Example :
#include <stdio.h>

int main() {
  int num = 10;

  printf("The value of num is : %d\n", num);
  printf("The address of num is : %x\n", &num);
  return 0;
}
Output :

The value of num is : 10
The address of num is : d7cbe634

2. '*' at address operator :

The at address operator or pointer operator (or indirection operator/dereferencing operator) is the complement of &. It is unary operator that returns the value located at the address that follows. For example if ptr contains the memory address of the variable num then the below statement will return the value of num.
 *m

Declaration of Pointer Variable : 

A pointer declaration consists of a base type, an * (asterisk sign ), and the variable name. The general form of declaring a pointer variable is :
type *variable_name;
Where :
  • type is the base type or data type of the pointer and may be any valid type.
  • variable_name is name of the pointer variable.
The base type or data type of pointer defines what type of variables the pointer can point to. Technically, any type of pointer can point anywhere in memory. However, all pointer arithmetic is done relative to its base type, so it is important to declare the pointer correctly.

Example :
int *ptr;
float *fptr;
char *cptr;
double *cptr;
Also note that the void type pointer works with all data types, but is not often used.

Initialization of Pointer Variable :

Initialization is the process of assigning address of a variable to a pointer variable. The address of '&' operator is used to assign the address of variable to pointer.
pointer_variable = &variable;
At above statement the address of variable is assigned to the pointer variable.
ptr = &num;
Now lets see an example :
#include <stdio.h>

int main() {
  int num;
  int *ptr;
  ptr = &num;
}
Also Note that the pointer variable is always point to variables of same data type. At the time of variable declaration is pointer is not initialized to any value or address, then it is recommended to assign a NULL value to the pointer variable. A pointer which is assigned a NULL value is known as NULL pointer.

Example :
#include <stdio.h>

int main() 
{
  int *ptr = NULL;
  return 0;
}

Dereferencing of Pointer (Or using the pointer) :

Once a pointer has been assigned the address of a variable, to access the value of the variable, pointer is dereferenced, by using the at address '*' operator. For example :
*ptr;
The above statement gives the value of the memory address pointed by the pointer variable ptr.

Now lets see some examples of it.

Example 1 :
#include <stdio.h>

int main() {
  int num = 20;
  int *ptr;
  ptr = &num;
  printf("%d\n", *ptr);
  return 0;
}
Output :

20

The above example prints the value of num.

Example 2 :
#include <stdio.h>

int main() {
  int var = 50;
  int *ptr;
  ptr = &var;

  printf("Value of var : %d\n", *ptr);
  printf("Value of var : %d\n", *&var);
  printf("Address of var : %x\n", &var);
  printf("Address of var : %x\n", ptr);
  printf("Address of Pointer ptr : %x\n", &ptr);

  return 0;
}
Output :

Value of var : 50
Value of var : 50
Address of var : 7dc8e4ac
Address of var : 7dc8e4ac
Address of Pointer ptr : 7dc8e4b0


Next Topic :