What are Pointers in C?

A Pointers in C is a variable which stores the address of another variable i.e, it points to another variable.

Pointers are an important part of C language. pointers provide a powerful and flexible way to manipulate data. To understand pointers, we need to understand how computer main memory RAM in organized.

Memory Organization

The computers main memory RAM consists of a large number of sequential storage locations. Each memory location is identified by a unique address.

The addresses are numbered sequentially from 0 to some maximum depending upon the memory size. i.e., they are positive integer values.

When we run a program, the program code and program data occupy some part of the main memory.

When we use a variable in the program, the compiler sets aside(allocates) a memory location for that variable. It associates the location’s address with the variable name.

Example:

Consider the following statement int n=20;

When this statement executes, the compiler, reserves space in memory to hold an integer value. Associates the name ‘n’ with this memory location and stores the value 20 at this location.

Applications Of Pointers in C

Pointers are used to pass parameters by reference, i.e., the actual arguments can be modified.

All arrays are implemented as pointers in C language.

They are used for passing arrays and strings to functions.

Pointers in C are used for dynamic memory allocation where memory is allocated and released for a variable during run-time.

All file handling operations are performed using pointers.

The Pointers in C are used in complex data structures like linked lists, tree, graphs, etc.

The Basics of Pointers

Pointer Declaration:

Since a pointer is a variable, it has to be declared like any other variable before it can be used.

Syntax: data_type * pointer_name;

The data_type is any data type in C. It indicates the type of the variable that the pointer points to. The asterisk(*) is an indirection operator and pointer_name is a valid C identifier.

Examples: int * ptr; char *ptr_ch1, *ptr_ch2;

Pointer Definition:

A pointer must be assigned the address of a variable is obtained by the address-of operator(&). The pointer in C can be defined by a statement of the form.

pointer = &variable; OR pointer = arrayname;

Example:

ptr=&n; Assign address of variable n to ptr.

char *p=”ABCD”; The pointer p points to a string ABCD which is stored somewhere in memory.

Pointer Initialization

Assigning an address to a pointer during declaration is called initialization.

Syntax: data_type * pointer_name = address;

Example:

int *ptr=&n; Declare variable n and assign the address of n to ptr.

int *ptr=NULL; Declare ptr and initialize it to NULL.

Pointer Dereferencing:

To use a pointer, it has to be dereferenced. De-referencing operation accesses data in the memory address which the pointer holds. The dereferencing operator is *(indirection operator). It is also called the value-at operator.

Syntax:

*pointer_name

Example:

int n=20, *ptr;

ptr= &n;

printf(“%d”, *ptr);

Here, ptr gives the address of variable n. *ptr gives the value at that address.

int n=20; int *ptr; ptr is an uninitialized pointer.

ptr=&n; Store address of n in ptr.

Examples of Pointers in C:

Program 1: Write a program to swap two numbers without using third variable.

<div class="wp-block-codemirror-blocks-code-block code-block">
<pre>//Learnprogramo
#include<stdio.h>  
int main(){  
int a=10,b=20,*p1=&a,*p2=&b;  
printf("Before swap: *p1=%%d *p2=%%d",*p1,*p2);  
*p1=*p1+*p2;  
*p2=*p1-*p2;  
*p1=*p1-*p2;  
printf("\nAfter swap: *p1=%%d *p2=%%d",*p1,*p2);  
return 0;  
}  </pre>
</div>

Output:

pointers in c

Program 2:

<div class="wp-block-codemirror-blocks-code-block code-block">
<pre>//Learnprogramo
#include <stdio.h>
int main()
{
int* pc, c;
c = 22;
printf("Address of c: %%p\n", &c);
printf("Value of c: %%d\n\n", c);  
pc = &c;
printf("Address of pointer pc: %%p\n", pc);
printf("Content of pointer pc: %%d\n\n", *pc); 
c = 11;
printf("Address of pointer pc: %%p\n", pc);
printf("Content of pointer pc: %%d\n\n", *pc); 
*pc = 2;
printf("Address of c: %%p\n", &c);
printf("Value of c: %%d\n\n", c); 
return 0;
}</pre>
</div>

Output:

pointers in c

Conclusion:

In this lesson, we have learned about pointers in c. Now in the next lesson, we will learn pointer arithmetic in c.

Also Read: