A derived data type that can store the address of other C variables or a memory location. Basically, a variable that stores the memory address of another variable. We can access and manipulate the data stored in that memory location using pointers. As the pointers store the memory address, their size is independent of the type of data they are pointing to. Pointers might sound complicated at first but just hand in there.
Syntax
int age =22;
int *tr=&age;
int _age=*ptr;
Declaring Pointers
int *ptr;
char *ptr;
float *ptr;
Format Specifier
printf("%p", &age);
printf("%p", ptr);
printf("%p", &ptr);
&=Value of address *= Address of
Note: If we write %p for the format specifier we get a hexadecimal value. And if we write %u we get it in the form of numbers.
Example
int main(){
int age=22;
int *ptr=& age;
// int _age=*ptr;
printf("ans=%p\n",& age); prints specifier
printf("%u\n",ptr); prints it in the form of numbers
printf("%d\n",age);
printf("%d\n",*ptr);
printf("%d\n",*(&age));
return 0;
}
Output :
Pointer to Pointer
A variable that stores the memory address of another pointer.
Syntax :
int **pptr;
char **pptr;
float **pptr;
Note: When you want to take the value you need " * ". And if you want to take the address you need " & ".
Q1 Print the value of ' i ' from its pointer to pointer in C.
#include<stdio.h>
#include<math.h>
int main(){
int i=5;
int *ptr=&i;
int **pptr=&ptr;
printf("%d\n",**pptr);
return 0;
}
Output :
Pointers in Function Call
Call by value
We pass value of variable as argument
Ex
void square(int n);
int main(){
int number=4;
square(number);
printf("number=%d",number);
return 0;
}
// call by value
void square(int n)
{
n=n*n;
printf("square=%d\n",n);
}
Output : square=16, number=14
Call by Reference
We pass address of variable as arguement.
Eg :
#include<stdio.h>
#include<math.h>
void square(int n);
void _square(int *n);
int main(){
int number=4;
square(number);
printf("number=%d\n",number);
_square(&number);
printf("number=%d\n",number);
return 0;
}
// call by value
void square(int n)
{
n=n*n;
printf("square=%d\n",n);
}
//call by reference
void _square(int *n)
{
*n=(*n) * (*n);
printf("number=%d\n",*n);
}
Output :
Q2 Swap 2 numbers A and B using pointers in C.
void swap(int *a, int *b);
int main(){
int x=3, y=5;
swap(&x, &y);
printf("number x=%dand \n y= %d\n",x,y);
return 0;
}
// call by reference
void swap(int *a,int *b)
{
int t=*a;
*a=*b;
*b=t;
}
Output :
Q3 Write a function to calculate the sum, product & average of 2 numbers. print the average in the main function. With pointers. In C.
#include<stdio.h>
#include<math.h>
void work(int a,int b,int *sum,int *prod,int *avg);
int main(){
int a=5,b=3;
int sum,prod,avg;
work(a,b,&sum,&prod,&avg);
printf("sum=%d,prod=%d,avg=%d",sum,prod,avg);
return 0;
}
void work(int a,int b,int *sum,int *prod,int *avg)
{
*sum=a+b;
*prod=a*b;
*avg=(a+b)/2;
}
Output :
Note: In this question A and B are pass by value and Sum Average and Product are pass by reference.
Hope you enjoyed learning about Pointers, this helps us in storing the address of a variable, hope this blog was useful. And don't forget to follow and share this blog with a friend who wants to learn about programming.
Thank you...