How to Use Pointers in C?
Using pointers in C involves understanding how to declare, assign, and dereference them. Here are the key steps and concepts:
- Declaring a Pointer: To declare a pointer, specify the data type it will point to, followed by an asterisk (*), and then the pointer name. For example:
int *ptr; // Declares a pointer to an integer
- Assigning a Pointer: To assign a pointer, set it to the address of a variable using the address-of operator (&). For example:
int num = 10;
int *ptr = # // ptr now points to the memory address of num
- Dereferencing a Pointer: To access the value stored at the memory address a pointer holds, use the dereference operator (*). This lets you read or modify the data at that address:
printf("%d", *ptr); // Outputs the value of num (10)
*ptr = 20; // Changes the value of num to 20
- Pointer Arithmetic: Pointers support arithmetic operations to navigate memory, such as incrementing (ptr++) or decrementing (ptr--) to move to the next or previous memory address for elements of the same data type.
- Null Pointer: A pointer can be set to NULL to indicate it does not currently point to any valid memory address:
int *ptr = NULL;
- Using Pointers with Arrays and Functions: Pointers are commonly used with arrays and functions to pass data efficiently. For example, passing an array or a large structure by reference to a function saves memory and time. Declares a pointer to an integer
Pointer in C Example
C
#include <stdio.h>
int main() {
int num = 10; // Declare an integer variable and assign it a value
int *ptr = # // Declare a pointer to int and assign it the address of num
printf("Address of num: %p\n", ptr); // Print the memory address stored in ptr (address of num)
printf("Value of num: %d\n", *ptr); // Dereference ptr to print the value of num
*ptr = 20; // Use ptr to change the value of num indirectly
printf("Updated value of num: %d\n", num); // Print the updated value of num
return 0;
}

You can also try this code with Online C Compiler
Run Code
Output
Address of num: 0x7ffeefbff5a4 // (example address, may vary each time)
Value of num: 10
Updated value of num: 20
Declaring a Pointer
Pointers are declared with the help of an asterisk (*) followed by the pointer's name.
Syntax
type *pointer_variable_name;
In the above syntax, type refers to what data type of the variable the pointer will point to. All the pointers, whether integer, float, character, etc., are of the same data type, a long hexadecimal number representing the memory address.
Pointers can be declared in two ways:
- One way is to attach the asterisk with the name of the pointer variable during the declaration
- Another way is to attach the asterisk at the end of the data type of which the pointer variable is to be created
Example
C
#include<stdio.h>
int main()
{
int x=10,y=42;
// using the 1st way
int* pointer_x=&x;
// using the 2nd way
int *pointer_y=&y;
printf("Value of x from the pointer variable: %d",*pointer_x);
printf("\nValue of y from the pointer variable: %d",*pointer_y);
}

You can also try this code with Online C Compiler
Run Code
Output
Value of x from the pointer variable: 10
Value of y from the pointer variable: 42
Working with pointers
When working with pointers, a few things should be taken into consideration:
- Defining a pointer variable before use
- Assigning the address of a variable to the pointer and not just the variable’s value. The variable which is to be pointed must be declared and initialized before assigning it to the pointer.
- While using the pointer to manipulate the variable’s value (*) must be used.
Example
C
#include<stdio.h>
int main()
{
int x=45;
// pointer declaration
int *pointer_x;
// assigning the pointer to variable
pointer_x=&x;
//using the (&) operator to access the address of variable
printf("The address at which the variable is stored : %x\n", &x);
//using the pointer to access the address of variable
printf("The address at which the variable is stored : %x\n",pointer_x);
//Manipulating the value of variable using pointer with help of (*) operator
printf("Original value of the variable using pointer : %d\n", *pointer_x);
*pointer_x+=1;
printf("Incremented value of the variable using pointer : %d\n", *pointer_x);
}

You can also try this code with Online C Compiler
Run Code
Output
The address at which the variable is stored : b3f9441c
The address at which the variable is stored : b3f9441c
Original value of the variable using pointer : 45
Incremented value of the variable using pointer : 46
NULL pointers
When declaring a pointer, it is a good practice to assign NULL value to it when we do not know the address which is to be assigned to it. A pointer that is assigned a NULL value is called a NULL pointer. The value of a NULL pointer is zero.
Example
C
#include<stdio.h>
int main()
{
int *pointer=NULL;
if(!pointer){
printf("This is a Null pointer with value : %d\n",pointer);
}else{
printf("This is not a Null pointer with value : %d\n",*pointer);
}
}

You can also try this code with Online C Compiler
Run Code
Output
This is a Null pointer with value : 0
Also see, Short int in C Programming
Address Of (&) Operator
The Address Of (&) operator in C is used to obtain the memory address of a variable. When you apply the & operator to a variable, it returns the address where that variable is stored in memory. This is particularly useful when working with pointers, as it allows you to store the address of a variable in a pointer.
Example:
C
#include <stdio.h>
int main() {
int num = 10;
int *ptr = #
printf("Value of num: %d\n", num);
printf("Address of num: %p\n", &num);
printf("Value stored in ptr: %p\n", ptr);
printf("Value pointed to by ptr: %d\n", *ptr);
return 0;
}

You can also try this code with Online C Compiler
Run Code
Output:
Value of num: 10
Address of num: 0x7ffc34d22a44
Value stored in ptr: 0x7ffc34d22a44
Value pointed to by ptr: 10
Pointer Arithmetic
A set of arithmetic operators can be performed on pointer like:
- Increment (++) & Decrement (--).
- Integer Addition or Subtraction to/from a pointer.
Example
C
#include<stdio.h>
int main()
{
int x=47;
// declare the pointer
int *ptr_x;
// assign the variable to pointer
ptr_x=&x;
printf("Original value of variable : %d\n",*ptr_x);
// increment the variable
++(*ptr_x);
printf("Value of variable after incrementing : %d\n",*ptr_x);
// decrement the variable
--(*ptr_x);
printf("Value of variable after decrementing : %d\n",*ptr_x);
// adding an integer to the variable
*ptr_x+=10;
printf("Value of variable after adding 10 to it : %d\n",*ptr_x);
// subtracting an integer to the variable
*ptr_x-=10;
printf("Value of variable after subtracting 10 from it : %d\n",*ptr_x);
}

You can also try this code with Online C Compiler
Run Code
Output
Original value of variable : 47
Value of variable after incrementing : 48
Value of variable after decrementing : 47
Value of variable after adding 10 to it : 57
Value of variable after subtracting 10 from it : 47
You can also read about the dynamic arrays in c, And Tribonacci Series
Print an array using a pointer
Example
C
#include<stdio.h>
int main()
{
int a[5]={5,4,6,8,9};
int *pointer=&a[0];
// printing array using pointer
printf("Elements of array are : ");
for(int i=0;i<5;i++)
printf("%d ",*(pointer++));
}

You can also try this code with Online C Compiler
Run Code
Output
Elements of array are : 5 4 6 8 9
Explanation
In the above example, we have assigned the address of the first element a[0]’s address to the pointer variable. Using the (*) asterisk operator, we print the current element's value, and using the post-increment operator (++), we update the index of the array pointed by the pointer.
Must Read Passing Arrays to Function in C
Passing and Returning Pointers to a Function
C programming language allows the user to pass and return pointers to a function. It is not recommended to return the address of local variable anywhere outside the function as it goes out of scope after the function returns.
Example
C
#include <stdio.h>
char* initialize()
{
static char ch = 'x';
return (&ch);
}
int main()
{
// Declare a pointer of char type
char* ch;
ch = initialize();
// Print Address
printf("Address at which character is stored : %p\n", ch);
// Print value
printf("The value stored at the above address: %c\n", *ch);
}

You can also try this code with Online C Compiler
Run Code
Output
Address at which character is stored : 0x107c76010
The value stored at the above address: x
Explanation
In the above code, a static variable is declared in the initialize() function because a stack is created for function calls by the compiler, when a function exits, the function stack gets removed. The local variables of that function goes out of scope except for static variables which have a special property of preserving their values even when they are out of scope.
Features of pointers in c
- Using pointers helps us save memory space.
- The memory of pointers is dynamically allocated that is, the memory pointed by pointers can be assigned and released efficiently and effectively.
- Execution time with pointers is faster as compared to normal variables as with pointers, we directly access the memory location and manipulate the data.
- Many data structures like two-dimensional and multidimensional arrays are represented efficiently using pointers.
- Pointers are used for file handling.
Try it once on online C compiler for better practice.
Must Read what is storage class in c and Decision Making in C
Frequently Asked Questions
What is the difference between a pointer and an ordinary variable?
The difference between a pointer and an ordinary variable is that in an ordinary variable, data of some type is stored, which depends on the data type of that variable, whereas a pointer of any data type stores a hexadecimal number that represents the memory address.
What is the difference between an uninitialized pointer and a null pointer?
The difference between an uninitialized pointer and a null pointer is that an uninitialized pointer points to an unknown memory location, and its behavior is undefined, whereas a null pointer is a pointer that has a null value and the behavior of a null pointer is defined.
What is the difference between a constant pointer and a pointer to a constant?
The difference between a constant pointer and a pointer to a constant is that a constant pointer is a pointer whose value is not modifiable, and if we try to modify it, we get a compilation error, whereas, in the case of pointer to a constant, the value of the pointed address is constant that means we can not change the value of the address that is pointed by the pointer.
Conclusion
In this blog, we have covered Pointers in C. Pointers are an essential and powerful feature of the C programming language, offering a unique level of control over memory management. They enable efficient manipulation of data structures, dynamic memory allocation, and enhance the performance of programs by allowing direct access to memory locations.
Data structures like Doubly Linked List and many others use pointers for referencing and other purposes, to learn about doubly linked lists, refer to this.
Using just two pointers, we can clone a linked list to learn and understand how to refer to this.