< Prev
Next >

# C - Pointers

C language has provided us a very important feature known as - Pointers. Pointer is a variable that stores a memory address of another variable. By having an access to a memory address, we could access the original value stored at it. In order to understand the concept of pointers, let's see a line of code -

``int a = 10;``

This line of code tells the compiler to reserve a space in the memory location, store an int value(10) in it, and name this memory location, a. This memory location where the int value(10) is stored is also referred to as a memory address.

Let's suppose this memory address is 2686776(computer automatically finds an empty memory address to store a value). Look at the diagram below for reference.

Pointers allows us to access and point to a particular address of a memory location. And, once we know the address of a variable, we could access the original value stored at this particular memory address and could even make permanent changes to this value by using these two operators:
• & operator/address of operator, which gives us the memory address of a variable.
• * operator/value at address operator, which gives us the value at a memory address.

## An example of Pointer

In the upcoming code example, we are going to create a pointer variable which points to a memory address, containing an int value 10.

``````/* C - Example of Pointer */

#include<stdio.h>

int main()
{
int a = 10;

/*Declaring a pointer variable b, which points to the address of int variable a */
int *b = &a;

printf("The value of a is : %d \n", a);
printf("The address of a is : %u \n", &a);
printf("The value at address pointed by pointer b is : %d \n", *b);
printf("The address pointed by pointer variable b is : %d ", b);

return 0;
}``````

## Output

``````The value of a is : 10
The address of a is : 868677
The value at address pointed by pointer b is : 10
The address pointed by pointer variable b is : 868677``````

## Program analysis

• We have declared a pointer variable, b, which is pointing to the address of an int variable a.
`int *b = &a;` Note: To declare a pointer variable, always use * before the name of a pointer variable.

• Next, we have accessed the address of int variable a, by using the & "address of" operator i.e. &a. This memory address is printed by using the %u format specifier.
`printf("The address of a is : %u \n", &a);`

• Next, we have accessed the int value(10) stored in a by using * "value at address" operator with the pointer variable, b, i.e. *b.
`printf("The value at address pointed by pointer b is : %d \n", *b);`

• Finally, we have printed the address pointed the pointer variable, b.
`printf("The address pointed by pointer variable b is : %d ", b);`

## Using pointer to modify a value passed to a function by reference

In the upcoming example, we are going to create a function named subtract10 which subtracts 10 from value of the variable of an int type, which is passed to this function by using a pointer when this function is called.

``````/* C - Calling a function with argument, by reference */

#include<stdio.h>

int main()
{

/*subtract10 function prototype declaration*/
void subtract10(int *);

int a = 10;
printf("The value in a is : %d \n", a);

/*calling the subtract10 function*/
subtract10(&a);

printf("After the subtract10 function is called \n");
printf("The value in a is : %d \n", a);
return 0;
}

/* function to subtract 10 from the int value passed to it */
void subtract10(int *i)
{
*i = *i - 10;
}``````

## Output-

``````The value in a is : 10
After the subtract10 function is called
The value in a is : 0``````

## Program Analysis

• Declaring function prototype - In the above mentioned example, we have created a function named subtract10, and have declared its prototype as -

``````/*subtract10 function prototype declaration*/
void subtract10(int *); ``````

The prototype of this function is declared with the void return type, because this method does not return any value once it finishes its execution. This function will be passed the address of an int value when it is called, therefore we have specified a pointer within the parenthesis(), that points to a value of int type i.e. int *

• Calling the function - We have called the subtract10 method by passing the address of value of an int variable named a as an argument, by using the "address of" operator i.e. &. The int variable a has a value 10 and its address is referred to as an "actual argument" of the function subtract10, when it is called.

`` subtract10(&a);	 /* subtract10 function is called */``

• Function definition - When the subtract10 function is called, it receives the address of the variable(a) passed to it and this address is copied into pointer variable named i, which is used to store the address of an int variable, declared in the definition of subtract10 function.

``````/* subtract10 function is defined */
void subtract10(int *i)
{
*i = *i + 10; /* using * to accessing the value at address */
}``````

In the function definition, the pointer variable i can also be referred as a "formal argument" of subtract10 function. This function uses the "value at address" * operator , which when used with a pointer variable i.e. i, allows us to access the value at an address in the memory location, pointed by i variable.

Eventually, the subtract10 function subtracts 10 from the value contained in i, which is reflected when the value of variable a is printed again on the console after calling the subtract10 function.

## Another example of pointers

• In the upcoming example, we are going to create a function named swap_int which accepts two int arguments and successfully swaps their values because we have passed the arguments to this function by reference i.e by using pointers. Let's see the code.

``````/* C - Calling a function with arguments, by reference */

#include<stdio.h>
int main()
{
void swap_int(int *,int *); /* function prototype declaration*/

int a = 10;
int b = 20;
printf("The value in a is : %d \n", a);
printf("The value in b is : %d \n", b);

/*calling the swap_int function*/
swap_int(&a,&b);

printf("After the swap_int function is called \n");
printf("The value in a is : %d \n", a);
printf("The value in b is : %d \n", b);
return 0;
}

/* A function to swap integers */
void swap_int(int *c, int *d)
{
int e;

e = *c;
*c = *d;
*d = e;
}``````

## Output-

``````The value in a is : 10
The value in b is : 20
After the swap_int() function is called
The int value in a is : 20
The int value in b is : 10```
```