Pointers in C
Introduction
C programming language allows the users to access and manipulate the memory occupied by a variable with the help of pointers. Pointers are variables that store the address of other variables. Like ordinary variables, pointers also have a data type and must be declared before they are used to store the address of any variable.
Also Read About, Sum of Digits in C
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
#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);
}
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
#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);
}
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
#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);
}
}
Output
This is a Null pointer with value : 0
Pointer Arithmetic
A set of arithmetic operators can be performed on pointer like:
- Increment (++) & Decrement (--).
- Integer Addition or Subtraction to/from a pointer.
Example
#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);
}
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.
Print an array using pointer
Example
#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++));
}
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.
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
#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);
}
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.
Try it once on online C compiler for better practice.
Features of pointers
- 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.
FAQs
- 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.
Key Takeaways:
In this blog, we have covered the following topics:
- We first discussed what pointers are how can we declare and initialize them?
- Then we discussed the pointer arithmetic and features of pointers.
Pointers play a very important role in the C/C++ programming language, to understand pointers and references in-depth, refer to this.
Data structures like doubly linked lists 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.