Introduction to Pointers in C language

“There is no elevator to success, You have to take stairs”

Hello everyone!!


Today we are going to discuss Pointers in C language.

This article will include the following points:-

1) Definition
2) Syntax
3) Diagrammatic Representation
4) Initialization and Declaration
5) Dereference/indirection operator
6) Advantages and
7) Applications of pointers.

Let’s start quickly 🙂

What are Pointers:- A pointer is a variable which stores the address of another variable.

Syntax of declaring pointer is:-

data type *pointervariable; 

For Example:- int *p; It declares pointer p pointing to integer variable.

Note:- Pointer also has its own address.

Diagrammatic Representation of Pointer:-

In the diagram below i is a variable having value 5 and address 65524.

j is a pointer variable which holds the address of variable i. i.e 65524 and also j is having its own address in memory.

C language - representation of Pointers
Representation of Pointer

Initialization of C Pointer variable:-

  • Pointer Initialization is the process of assigning address of a variable to a pointer variable.
  • It contains the address of a variable of the same data type.
  • In C language address operator & is used to determine the address of a variable. The & (immediately preceding a variable name) returns the address of the variable associated with it.

Pointer Declaration and Initialization:-

int a = 10;
int *ptr; //pointer declaration
ptr = &a; //pointer initialization .

Pointer variable always points to variables of the same datatype.

Pointer Initialization and Declaration

Dereference/Indirection Operator:-

  • The dereference operator is also known as an indirection operator, which is represented by (*).
  • When indirection operator (*) is used with the pointer variable, then it is known as dereferencing a pointer.
  • When we dereference a pointer, then the value of the variable pointed by this pointer will be returned.

Examples of Dereferencing:-

Q1) C program used to change the value stored at pointer variable.

#include<stdio.h>  

int main()  
{  
 int x=9;  
 int *ptr; //Declaration of pointer variable
 
 ptr=&x; // Address of variable x is stored in ptr pointer variable 
 *ptr=8; //This statement is used to change the value stored at pointer variable  
 printf("value of x is : %d", x);  
}  

Output:- 8

Q2) C program to access the value using the pointer.

#include<stdio.h> 
int main ( )  
{ 
 int a = 20; 
 int *p;  
 p = &a; 
 printf("Address of variable: %u\n", &a );  
 printf("Address stored in variable: %u\n", p );  
 printf("Value of variable: %d\n", *p ); 
} 

Output:-
Address of variable: 1464212924
Address stored in variable: 1464212924
Value of variable: 2

Advantage of pointer:-

  • Pointer reduces the code and improves the performance, it is used to retrieving strings etc. and used with arrays, structures, and functions.
  • We can return multiple values from a function using the pointer.
  • It makes you able to access any memory location in the computer’s memory.

Applications of pointer:- There are many applications of pointers in C language.

  • Dynamic memory allocation.
    • In C language, we can dynamically allocate memory using malloc() and calloc() functions where the pointer is used.
  • Arrays, Functions, and Structures.
    • Pointers in C language are widely used in arrays, functions, and structures. It reduces the code and improves the performance.

Soo this is a short introduction of Pointers in C language. We will discuss Pointers in detail in upcoming articles.

Keep reading keep learning.


Thank you for reading 🙂 Have a wonderful day ahead 🙂

You may also like...

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from Microsoft 365

Subscribe now to keep reading and get access to the full archive.

Continue reading