Pointers in C Language

Pointers are very important and powerful structures that can be used by C programmers because they allow you to easily work with variables, functions, data structures and algorithms through their memory addresses.

A Pointer is a variable that points to an address of a value. In other words, it “points” to the location assigned to a variable and can directly access the variable.

Symbols used in pointer

Symbol

Name

Description

& (ampersand sign)

address of operator

It determine the address the address of a variable

* (asterisk sign)

indirection operator/value at address

Access the value at the address

Syntax

Pointers are declared using the * symbol and take the form:

1

2

//pointer to datatype

datatype *variable_name;

Where * (asterisk sign) is used to denote that “variable_name” is pointer variable and not a normal variable.

Declaring and Initializing Pointer Variables

Pointer variables must be declared before they can be used, as shown in the following example:

1

2

3

intx=0;

intiAge=30;

int*ptrAge;

Simply place the indirection operator (*) in front of the variable name to declare a pointer.

In this example, we have declared three variables, two integer variables, and one pointer variable. For readability purposes, I use the naming convention ptr as a prefix. This helps us and other programmers identify this variable as a pointer.

Note: Naming conventions, such as ptr, are not required. Variable names and naming conventions do not matter in C. They simply help you identify the data type of the variable and, if possible, the purpose of the variable.

When we declared the pointer ptrAge, It was telling C that I want my pointer variable to indirectly point to an integer data type. The pointer variable, however, is not pointing to anything just yet. To indirectly reference a value through a pointer, you must assign an address to the pointer, as shown here:

1

ptrAge=&iAge;

In above example, we have assigned the memory address of the iAge variable to the pointer variable ptrAge.

We have placed the unary operator (&) in front of the variable iAge. This statement is telling C that I want to assign the memory address of iAge to my pointer variable ptrAge.

The unary operator (&) is often referred to as the “address of” operator because, the pointer ptrAge is receiving the “address of” iAge.

Conversely, we can assign the contents of what my pointer variable points to a non-pointer data value as demonstrated next.

1

x=*ptrAge;

The variable x will now contain the integer value of what ptrAge points to the integer value 30.

Note that, not initializing your pointer variables can result in invalid data or invalid expression results. Pointer variables should always be initialized with another variable’s memory address, with 0, or with the keyword NULL.

1

2

3

4

5

6

7

int*ptr1;

int*ptr2;

int*ptr3;

ptr1=&x;

ptr2=0;

ptr3=NULL;

Remembering that pointer variables can only be assigned memory addresses, 0, or the NULL value is the first step in learning to work with pointers.

A pointer that is not assigned any value but NULL is known as NULL pointer. If you don’t have any address to be specified in the pointer at the time of declaration, you can assign NULL value. It will a better approach.

Examples of pointer

Let’s see the simple example that demonstrates the use of * for pointers in C.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

#include <stdio.h>

intmain()

{

// Normal integer variable

intVar=20;

// Pointer variable that holds address of var.

int*ptr=&Var;

// This line prints value at address stored in ptr.

// Value stored is value of variable "var"

printf("Value of Var = %d\n",*ptr);

// The output of this line is machine dependent

printf("Address of Var = %p\n",ptr);

*ptr=30;// Value at address is now 30

// This will print 30

printf("Now value of *ptr is %d\n",*ptr);

return0;

}

When you run above program, the output will be following:

1

2

3

Value of Var = 20

Address of Var = 0x7fffb108ea3c

Now value of *ptr is 30

Advantage of pointer

1) Pointer reduces the code and improves the performance, it is used to retrieving strings, trees etc. and used with arrays, structures, and functions.

2) We can return multiple values from function using pointer.

3) It makes you able to access any memory location in the computer’s memory.

Usage of pointer

There are many usages of pointers in c language.

1) Dynamic memory allocation

In c language, we can dynamically allocate memory using malloc() and calloc() functions where the pointer is used.

2) Arrays, Functions, and Structures

Pointers in c language are widely used in arrays, functions and structures. It reduces the code and improves the performance.