Pointer, Types of Pointer with examples

 

Hello friends how are you, today in this blog i will teach you what is pointer, syntax of pointer , how pointer statement works and many programs using pointer in a very simple way.

Let's start

1.It is a special type of variable which is used to store the address of another variable.
2.It can store the address of same data type means an integer pointer can store the address of integer variable, character pointer can store the address of character variable and so on.
3.If we add asterisk(*) symbol with any variable at the time of declaring variable then this variable is called pointer variable.
4.we use ampersand symbol to get the address of variable.
5.* symbol is used to get the value at address which is hold by pointer

Syntax


1.Here a is a normal variable.
2.p is a pointer variable because it is associated with * symbol.

Example 1

#include<stdio.h>
int main()
{
int a=10;//initializing normal variable
int *p;//declaring pointer variable
p=&a;//address of variable a is assigned to p
printf("value of a=%d\n",a);
printf("address of a=%d\n",&a);
printf("value of p=%d\n",p);
printf("address of p=%d\n",&p);
printf("value of *p=%d\n",*p);
}
/*
### Output ###
value of a=10
address of a=6618652
value of p=6618652
address of p=6618640
value of *p=10
*/

Example 2:Addition Program using pointer

#include<stdio.h>
int main()
{
int a,b=10,c=20;//initializing normal variable
int *p,*q,*r;//declaring pointer variable
p=&a;//address of variable a is assigned to p
q=&b;//address of variable b is assigned to q
r=&c;//address of variable c is assigned to r
*p=*q+*r;//Performing operation
printf("Add=%d",*p);
}
/*
### Output ###
Add=30
*/

Example 2:User input in pointer

#include<stdio.h>
int main()
{
int a,b,c;//initializing normal variable
int *p,*q,*r;//declaring pointer variable
p=&a;//address of variable a is assigned to p
q=&b;//address of variable b is assigned to q
r=&c;//address of variable c is assigned to r
//Taking user input
printf("Enter first number\n");
scanf("%d",q);
printf("Enter second number\n");
scanf("%d",r);

*p=*q+*r;//Performing operation
printf("Add=%d",*p);
}
/*
### Output ###
Enter first number
50
Enter second number
40
Add=90
*/

Example 3:Pointer to Pointer

#include<stdio.h>
int main()
{
int a=10;//initializing normal variable
int *p,**q,***r,****s;//declaring pointer variable
p=&a;//address of variable a is assigned to p
q=&p;//address of pointer p is assigned to q
r=&q;//address of pointer q is assigned to r
s=&r;//address of pointer r is assigned to s
printf("a=%d\n",a);
printf("*p=%d\n",*p);
printf("**q=%d\n",**q);
printf("***r=%d\n",***r);
printf("****s=%d\n",****s);
}
/*
### Output ###
a=10
*p=10
**q=10
***r=10
****s=10
*/


Character Pointer
#include<stdio.h>
int main()
{
char a='Z';//initializing normal variable
char *p;//declaring pointer variable
p=&a;//address of variable a is assigned to p
printf("value of a=%c\n",a);
printf("Value  of *p=%c\n",*p);
}
/*
### Output ###
value of a=Z
Value  of *p=Z
*/


Float Pointer

#include<stdio.h>
int main()
{
float a=85.4;//initializing normal variable
float *p;//declaring pointer variable
p=&a;//address of variable a is assigned to p
printf("value of a=%f\n",a);
printf("Value  of *p=%f\n",*p);
}
/*
### Output ###
value of a=85.400002
Value  of *p=85.400002
*/

Here I am requesting if you found this post was helpful for you then let me know by your comment in below comment box and share it with your friend. Also you can contact me on Instagram @rajusunagar_

If you have not subscribed my website then please subscribe my website. Try to learn something new and teach something new to other. 

Thank you!!.......


Post a Comment

0 Comments