Pointers In C
Hey there! In this article, we will discuss C pointers in detail, their types, uses, advantages, and disadvantages with examples.
- In C, the pointers are used to store the address of another variable and can also be used to access and manipulate the variable's data stored at that location.
- Pointers can also store the addresses of functions, array elements and even the addresses of another pointer
- With pointers, we can access and modify the data located in the memory, pass the data efficiently between the functions, and create dynamic data structures like linked lists, trees, and graphs.
Video Explanationโ

1. Pointer Declaration :โ
To declare a pointer, use the dereferencing operator (*) followed by the data type.
Syntax :โ
The syntax below is used to define a pointer to a variable of a certain data type
data_type *ptr;
- ptr is the name of the pointer
- data_type is the name of the data type its pointing to
Example :โ
int *ptr;
The pointer declared here will point to some random memory address as it is not initialized.
2. Pointer Initialization :โ
Pointer initialization is the process where we assign some initial value to the pointer variable. We generally use the ( &: ampersand ) address of operator to get the memory address of a variable and then store it in the pointer variable.
Example :โ
int num = 24;
int *ptr; //declaration of an integer pointer
ptr = #
We can also declare a pointer in a single line,like below :
int *ptr = # //pointer definition
In Pointer Definition a pointer can be declared & initialized at the same time.
3. Pointer Dereferencing :โ
Dereferencing a pointer is the process of accessing the value stored in the memory address specified in the pointer. We use the same ( * ) dereferencing operator that we used in the pointer declaration.
int num =20;
int *ptr = #
printf("%d\n",*ptr); //OUTPUT : 20
printf("%x\n",ptr); //OUTPUT : 81b694b4 | Address of the 'num' variable (Address of a variable can be accessed via '&' operator)
*ptr = 34; //changing the value of num via dereferencing
printf("%d\n",*ptr); // OUTPUT : 34
C Program to illustrate pointers :โ
#include <stdio.h>
void main()
{
int num = 10;
// declare pointer variable
int* ptr;
//data type of ptr and num must be same
ptr = # // pointer initialization
printf("Value at ptr = %p \n", ptr); //Address of 'num'
printf("Value at num = %d \n", num);
printf("Value at *ptr = %d \n", *ptr);
*ptr = 24;
printf("Value at num = %d \n", num);
printf("Value at *ptr = %d \n", *ptr);
}
Output :โ
Value at ptr = 0x7ffd1070ce84
Value at num = 10
Value at *ptr = 10
Value at num = 24
Value at *ptr = 24
4. Size of a Pointer :โ
The memory (or, size) occupied by a pointer variable does not depend on the type of the variable it is pointing to. The size of a pointer depends on the system architecture.
The size of pointers in C is :
- 8 bytes for a 64-bit System
- 4 bytes for a 32-bit System
In the below example, we are printing the size of different types of pointers (64-bit architechture):