Pointers in C.
Hello Everyone,
Today we will discuss about Pointer in C program. In Simple word Pointer in C is special type of variables which holds memory address of any variable. Before going to know more about Pointer . Please see my below blog where in archive folder where you will able to find all the previous topics.
dheeraj60.blogspot.com
Let’s Start with Pointer in C . Pointer is basically used to see the address of variable which we assigned in the program. In other word we can say A pointer is a variable whose value is the address of another variable, i.e., direct address of the memory location. Like any variable or constant, you must declare a pointer before using it to store any variable address. Suppose we declare any variable of int type like int i=20; and I want to see the address of that value then through pointer we can easily see the address of variable i which stored in the memory location with different format.
Syntax of Declaring Pointer is :
Int *p; -- for integer data type
double *p; - for double data type
float *p ; - For float data type
char * Ch – for character data type
Note : Actual data type of the value of all pointers, whether it is integer, float, double or character is saved in a long hexadecimal number that represents a memory address of variables.
Let’s understand with one simple Program where we use integer data type and assigned that value and get the address by using Pointer.
Program is as below:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a = 100;
int *p;
p=&a;
printf("value of a is : %d\n", a);
printf("Address of variable a is %p", *p);
return 0;
}
Here in above program we declare pointer int *p; and already assigned the value of variable a=100 and address of variable assigned in p and through print f command we display the variable value with its address also by using pointer by using %p in printf as we use %d for int data type.
Below is the output of above code in Compiler Code Block with both variable values with its address location.
Let’s write one more Program where we can see the values of variables with address by using pointer with same concept.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a = 100;
int *p;
p = &a;
printf("Address of var variable: %x\n", &a );
printf("Address stored in p variable: %x\n", p );
printf("Value of *p variable: %d\n", *p );
}
Here actual variable declaration of a is 100 then we declare pointer *p then we store the address of a in pointer variable by using p = &a . in second print f statement we print address stored in pointer variable and in last print f we access the value using the pointer.
Below is the output of above Program in compiler code Block .
Here you can easily trace the address of variable and can access the actual value using the pointer. Hope you are able to understand the basics of Pointer.
Let’s understand one more program by using Array in pointer to see the output of value with address.
Program is Below
#include <stdio.h>
#include <stdlib.h>
int main()
{
int marks[] = {70,80,90};
int *p[3],i;
for (i=0; i<3; i++)
{
printf("%d\n",marks[i]);
p[i]=&marks[i];
}
for (i=0; i<3; i++)
{
printf("%d",*p[i]);
}
return 0;
}
Here through array we can display the values of marks and through pointer we also show the address of variable.
Please see the output of above program in compiler code block for your understanding.
See the output as we can see the marks and address of variable which we used to see by pointer using array.
Hope you are able to understand the basic concept of Pointer and can write programs to get the address of any variables etc by using simple as well as array program so please practice as much as you can.Pointer is a big topic which includes Pointers to pointers and pointers used in functions etc which I am going to cover in next blog.
Please read this blog carefully so that it’s easy for you to understand more topics in Pointers with Programs and it’s uses and benefits.
Thanks
Good topic for beginners in IT field.
ReplyDelete