C Pointers
C Pointers
Pointers (pointer variables) are special variables that are used to store addresses of another variable.
It is capable of storing the initial address of the object which is want to point to. The use of pointers
allows low-level memory access, dynamic memory allocation.
Syntax of C Pointers
datatype * ptr;
where
1. Pointer Declaration
2. Pointer Initialization
3. Pointer Dereferencing
1. Pointer Declaration
To declare a pointer, we use the ( * ) dereference operator or indirection operator before its name.
Example
int *ptr;
2. Pointer Initialization
One way to initialize a pointer is to assign address of some variable.
Example
or
3. Pointer Dereferencing
Dereferencing a pointer means accessing the value stored at the memory address that the pointer is
pointing to. This is done using the * indirection operator in front of the pointer.
Example
int x = 10;
Types of Pointers in C
1. Null pointer
The Null Pointers are those pointers that do not point to any memory location. They can be
created by assigning a NULL value to the pointer. A pointer of any type can be assigned the
NULL value.
Syntax
data_type *pointer_name = NULL;
or
pointer_name = NULL
Example
Int *ptr = NULL;
2. Generic pointer or void pointer
It means that they do not have any associated data type. The generic can point to any type
and can be typecasted to any type. One of the main properties of void pointers is that they
cannot be dereferenced.
– We need to cast a void pointer to another kind of pointer before using it.
– Generic pointers are used when a pointer has to point to data of different types at
different times.
Syntax
void * pointer_name;
Example
Int x=5;
Void *ptr;
To print x value using pointer we need to typecast the pointer ie. *(int*)ptr
Program
#include<stdio.h>
void main()
{
Int x=5;
char a=’X’;
void *ptr;
ptr=&x;
printf(“Value of x = %d”,*(int*)ptr);
ptr=&a;
printf(“Value of a = %c”,*(char*)ptr);
Syntax
datatype ** pointer_name;
Example :
int **ptr
double **ptr
int x = 5;
4. Function pointers
A pointer in C is a variable that stores the address of another variable. Similarly, a variable
that stores the address of a function is called a function pointer or a pointer to a function.
Function pointers can be useful when you want to call a function dynamically.
Declare syntax
function_return_type(*Pointer_name)(function argument list)
Example
#include <stdio.h>
int addition (int a, int b)
{
return a + b;
}
int main()
{
int (*ptr)(int, int) = addition;
int x = 10, y = 20;
int z = (*ptr)(x, y);
printf("Addition of x: %d and y: %d = %d", x, y, z);
return 0;
}
5. Array pointer
An array pointer is a pointer that points to the first element of an array. It allows you to
access array elements indirectly through pointer arithmetic or dereferencing.
Syntax
data_type (*var_name)[size_of_array];
Here:
Example
int (*ptr)[10];
(OR)
Program
#include <stdio.h>
int main()
return 0;
OUPUT
10
20
30
40
50
Pointer Arithmetic
Pointers (pointer variables) are special variables that are used to store addresses of another
variable.
C pointers arithmetic operations are different from the general arithmetic operations. The
following are some of the important pointer arithmetic operations in C:
• Subtraction of Pointers
• Comparison of Pointers
For Example:
If an integer pointer that stores address 1000 is incremented, then it will increment by 4(size
of an int), and the new address will point to 1004. While if a float type pointer is incremented
then it will increment by 4(size of a float) and the new address will be 1004.
Decrement: It is a condition that also comes under subtraction. When a pointer is decremented, it
actually decrements by the number equal to the size of the data type for which it is a pointer.
For Example:
If an integer pointer that stores address 1000 is decremented, then it will decrement by 4(size
of an int), and the new address will point to 996. While if a float type pointer is decremented
then it will decrement by 4(size of a float) and the new address will be 996.
2. Addition and subtraction of Integer to Pointer
When a pointer is added or subtracted with an integer value, the value is first multiplied by
the size of the data type and then added or subtracted to the pointer.
For Example: (Addition)
Consider the same example as above where the ptr is an integer pointer that stores 1000 as an
address. If we add integer 5 to it using the expression, ptr = ptr + 5, then, the final address
stored in the ptr will be ptr = 1000 + sizeof(int) * 5 = 1020.
For Example:
Two integer pointers say ptr1(address:1000) and ptr2(address:1004) are subtracted. The difference
between addresses is 4 bytes. Since the size of int is 4 bytes, therefore the increment between ptr1
and ptr2 is given by (4/4) = 1.
4. Comparison of Pointers
We can compare the two pointers by using the comparison operators in C. We can implement this
by using all operators in C >, >=, <, <=, ==, !=. It returns true for the valid condition and returns
false for the unsatisfied condition.
Example program
#include <stdio.h>
int main()
{
int a = 22;
int *p = &a;
// pointer increment and decrement
printf("p = %u\n", p);
p++;
printf("p++ = %u\n", p);
p--;
printf("p-- = %u\n", p);
int N = 4, X=6;
int *ptr1 = &N;
int *ptr2 = &X;
// Addition of 3 to ptr1
printf("Pointer ptr1 after Addition: ");
printf("%u \n", ptr1);
Ptr1 = ptr1 + 3;
printf("Pointer ptr1 after Addition: ");
printf("%u \n", ptr1);
// pointer to pointer subtraction
ptr2 = &X; ptr1 = &N;
x = ptr2 - ptr1;
printf("Subtraction of ptr1 “& ptr2 is %d\n", x);
}
OUTPUT
p = 6422308
p++ = 6422312
p-- = 6422308
Pointer ptr1 before Addition: 6422308
Pointer ptr1 after Addition: 6422320
Subtraction of ptr1 & ptr2 is 1