Pointer in C
Pointer
Pointers constitute a fundamental element of the C programming language. They serve the purpose of storing the memory addresses of various entities, including variables, functions, and even other pointers. The utilization of pointers facilitates low-level memory access, dynamic memory allocation, and a range of additional functionalities within C.A pointer variable points to a data type (like int) of the same type, and is created with the * operator.
syntax:- type *name
Declaration:- int *aoi;
//int is a type of pointer
//*aoi is a pointer variable.
program to print the address of variable using unary operator.
#include<stdio.h>
#include<conio.h>
#include<string.h>
main()
{
int i,j;
float f;
char p[20];
clrscr();
i=50;
j=60;
printf("\n The value of i is= %d",i);
printf("\n The address of i is= %u",&i);
printf("\n The value of j is= %d",j);
printf("\n The address of j is= %u",&j);
getch();
}
//name pointer:- value of variable through address.
#include<stdio.h>
#include<conio.h>
#include<string.h>
main()
{
int i,j;
float f;
char name[20];
clrscr();
i=50;
j=60;
printf("\n The value i is= %d",i);
printf("\n The value i through address is= %d",*(&i));
printf("\n The address i is= %u",&i);
printf("\n The value j is= %d",j);
printf("\n The value j through address is= %d",*(&j));
printf("\n The address j is= %u",&j);
printf("\n The value float is= %f",dec);
printf("\n The value float through address is= %f",*(&dec));
printf("\n The address float dec is= %u",&dec);
printf("\n The value char name is= %s",name);
printf("\n The value of name through address is= %s",*(&name));
printf("\n The address char name is= %u",&name);
getch();
}
//program to create a pointer variable using your name and use it.
#include<stdio.h>
#include<conio.h>
main()
{
int i, *pankaj; //*pankaj store the address.
clrscr();
i=50;
pankaj=&i;
printf("\n The value of i is %d",&i);
printf("\n The value of i through addres is%d",*pankaj);
printf("\n The address of i is %u",pankaj);
*pankaj=*pankaj+20;
printf("\n the original value is %d",i);
printf("\n the updated value is %d"*pankaj);
getch();
}
// pankaj shows the address of variable.
// *pankaj shows the value of variable.
// *pankaj+20 shows the updated value of variable.
//program to use pointer as an Array .
#include<stdio.h>
#include<conio.h>
#include<string.h>
main()
{
int i,n, *roll;
//you can use *roll variable as pointer variable to store addrss.
// OR use it as array of undefinite element.
// here we use *roll as array not pointer.
clrscr();
printf("\n How many records you want to enter");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\n Enter the roll number= ");
scanf("%d",&roll[i]);
}
for(i=0;i<n;i++)
{
printf("\n The roll number is %d",roll[i]);
}
getch();
}
Note:-
"&" (uniary operator) //returns only one operand
m=&count; //m has the address of count.
q=*m; //q has the value of count. //q recieves the value at address m.