What are pointers in C language?

In C language, the pointer is a special type of variable that is used to store the address of another variable.

There, another variable is a normal variable of any type e.g. int, float, char etc. For example, we have an int type variable with a value i.e.

int number = 10;

What does this line mean to the C compiler?

  • It reserves/allocates the space to hold the integer value in memory.
  • The variable name ‘number’ is associated with that address
  • In that variable name/allocated space, the value 10 is stored.

As we created this variable, C allocates a location in memory to this variable that has an address.

This is where a pointer comes; we store those addresses in the pointer variables.

Syntax of declaring a pointer

The syntax of the pointer is:

datatype *variable_name;

Where datatype can be int, float, char etc.

This is followed by a * and variable name.

An example of int pointer

In this example, we get the address of an int type variable. For that, we have a pointer and a normal int type variable. A value is assigned to that normal variable and then we got the address and displayed on the screen as shown below:

The code:

#include <stdio.h>

int main()

{

int* pnt; // A pointer variable

int num; //A normal variable




num = 5;

pnt = &num;




printf("Address of variable num is: %x", &pnt);

}

The result:

Address of variable num is: 28ff1c

Note: you should get a different result depending on the address allocation on your computer.

The example of char pointer

Similarly, you may declare a char type pointer to hold the address of a normal char variable. The example below shows how:

#include <stdio.h>

int main()

{

char* pnt; // A pointer variable

char chr; //A normal variable




chr = 'x';

pnt = &chr;

printf("The address is: %x" "\n", &pnt);

printf("Value stored in address: %c", *pnt);

}

The output of the above example:

The address is: 28ff1c

value stored in address: x

In the second line, we got the value stored in that address by using the pointer variable.

Changing the value by pointer variable example

In this example, we will change the value of the variable by using a pointer. The value of the normal variable is displayed by using a pointer. Then we changed the value by pointer variable and displayed it again:

The code:

#include <stdio.h>

int main()

{

int* pnt; // A pointer variable

int num; //A normal variable

num = 5;

int = &num;

printf("Initial value: %d \n", *pnt);

printf("Variable Address: %x \n", &pnt);




//Value changed

*pnt = 10;




printf("After modifying pointer variable: %d \n", *pnt);

printf("Variable Address: %x \n", &pnt);

The output:

C pointer

You saw the value is changed for the variable, however, the address remained the same.

An example of passing a pointer to a function

In the following example, we have a function where we will add the value by ten to the passed pointer. Have a look:

#include <stdio.h>

void additionP(int* ptr) {

  *ptr = *ptr + 10;

}




int main()

{

  int* p1, x = 5;

  p1 = &x;

  additionP(p1);




  printf("%d", *p1);

  return 0;

}

The output:

15

What are the uses of pointers?

The pointers in C can be used for different purposes. For example:

  • Dynamic memory allocation
  • To access the array elements
  • Passing the arguments by reference
  • For implementing data structures like linked lists, queues, stacks, trees, graphs etc.
  • In system-level programming, the memory addresses are useful – so pointers are used there.
  • Pointers are also used for file handling.

Few main points about the pointers

  • We use ‘&’ sign to get the address of a variable. The ‘&’ operator (ampersand) is also known as “Address of”
  • In order to get the value of a variable that pointer is pointing to, we use ‘*’ symbol. The ‘*’ (asterisk) Operator is also known as Value at address
  • For a 16 bit computer, the size of the pointer is 2 bytes.
  • Pointers can be subtracted but you cannot perform addition, multiplication, and division on pointer variables. The usage of subtraction is to know the elements between the two pointers.