Pointer in C
A pointer is a variable that stores the memory address of another
variable rather than the actual value.
> A normal variable stores data (e.g., an int stores a number).
> A pointer stores the location where that data is kept
Pointer size
Depends on system architecture, not the type it points to.
o 16-bit → 2 bytes
o 32-bit → 4 bytes
o 64-bit → 8 bytes
Address storage
Every byte in memory has a unique address.
A pointer stores the starting address(initial address) of the
variable it points to.
Declaring a pointer & Initializing a pointer:
Syntax:
data_type *pointer_name
Example:
int *p; // pointer to int
char *q; // pointer to char
float *r; // pointer to float
Note: * indicates it’s a pointer, and the data type tells what kind of
variable it can point to.
Assign the address of a variable using & Operator:
int a = 10;
int *p = &a; // p now holds address of a
Value of operator (or) Indirection operator (or) Dereference
operator:
>It is used to accessing the value stored at an address
>A pointer holds a memory address. To get the actual value stored
there, we dereference it.
Syntax:
*pointer_name
Example:
//dereference operator
#include<stdio.h>
int main()
{
int a=10;
int *ptr=&a;
printf("%p\n",ptr);//print the address
printf("%d",*ptr);//print the actual value
}
Output:
0x7ffd00cae2c4
10
>we can also change the value of the object pointed by the pointer
using dereference operator.
Example:
#include<stdio.h>
int main()
{
int a=10;
int *ptr=&a;
printf(" value is %d\n",*ptr);//print the actual value
*ptr = 20;//it assign the new value to a
printf(" value is %d",a);//here the value of a is changed
}
Output:
value of a is 10
value of a is 20
Use cases of value-of operator (*) in C:
Access the value stored at a pointer’s address.
Modify a variable through its pointer.
Implement pass-by-reference in functions.
Access array elements using pointer arithmetic.
Read/write values in dynamically allocated memory.
Access struct members via a pointer.
Pointer Assignment:
Example:
#include<stdio.h>
int main()
{
int a=10;
int *p = &a;
int *q;
q=p;//here q and p points the same memory location
printf("the value is:%d",*q);
}
Output:
the value is:10
Application of Pointer:
Example 1:
//1)changing the values in another function using pointer
#include<stdio.h>
void change(int arr[],int len)
{
int i;
for(i=0;i<len;i++)
{
arr[i]*=2;
}
}
int main()
{
int arr[]={1,2,3,4,5};
int len=(sizeof(arr)/sizeof(arr[0]));
change(arr,len);
int i;
for(i=0;i<len;i++)
{
printf("%d ",arr[i]);
}
}
Output:
2 4 6 8 10
Example 2:
#include<stdio.h>
void minmax(int arr[],int len,int *min,int *max)
{
int i;
for(i=1;i<len;i++)
{
if(arr[i]>*max)
{
*max=arr[i];
}
if(arr[i]<*min)
{
*min=arr[i];
}
}
}
int main()
{
int arr[]={4,6,7,2,8};
int len=sizeof(arr)/sizeof(arr[0]);
int min=arr[0];
int max=arr[0];
minmax(arr,len,&min,&max);
printf("min=%d max=%d",min,max);
}
Output:
min=2 max=8
Arrays and Pointers:
>An array name (e.g., arr) represent a pointer to its first element .
> That means arr is equivalent to &arr[0] when used in places like
function calls.
Example:
#include<stdio.h>
int main()
{
int arr[]={10,20,30,40,50};
int i;
int len = (sizeof(arr)/sizeof(arr[0]));//Nm of element in an array
for(i=0;i<len;i++)
{
printf("%d ",arr[i]); // print the element in the array
//printf("%d ",*(arr+i)); // print the element in the array
}
}
Output:
10 20 30 40 50
Note:
In this program arr[i] is equal to *(arr+i)