Like all other variables, pointer variables must be declared before their usage in C program. The interpretation of pointer variables is different from the interpretation of other variable declarations. The pointer variable is used to store the address of some other variable. The data type appears in the pointer variable declaration refers to the type of the data item whose address is to be stored in this pointer variable rather than the type of the pointer variable itself.
There are two special pointer operators: * and &. The & is a unary operator that returns the memory location of its operand. For example, the statement
ptr = &k;
would assign the memory location of variable ptr. The & operator is known as address of operator. This operator can be used only with normal variables. To understand the above statement, ptr will have the value 2001.
Program – Use of & operator
#include <stdio.h>
#include <conio.h>
void main ()
{
char c;
int n:
float f;
c = ‘X ‘;
n = 29;
f = 37. 345;
clrscr ();
printf (“%c is stored at address: %u\n”, c, &c);
printf (“%d is stored at address: %u\n”, n, &n);
printf (“%f is stored at address: %u\n”, f, &f);
}
Output
X is stored at address: 6684148
29 is stored at address: 6684144
37. 345000 is stored at address: 6684140
The second operator, *, is the complement of & operator. It is also an unary operator that returns the value of the variable located at the address that follow. For example, if the ptr contains the memory location of the variable k,
n = *ptr;
place the value of k into n. n will have the value 25 because 25 is stored at location 2001, which is the memory location that is stored in ptr. So * is called as value at address operator.
Program - *is complement of &
#include<stdio.h>
#include<conio.h>
void main ()
{
int n= 25;
clrscr ();
printf (“Address of variable n is %u\n”, &n);
printf (“value of variable n is %d\n”, n);
printf (“variable of variable n is %d\n”, *(&n));
getch ();
}
Output
Address of variable n is 6684148
Value of variable n is 25
Value of variable n is 25
Note that in this program, displaying the value of *(&n) is same as displaying the value of n.
The variable name must be preceded by an asterisk (*) symbol when a pointer variable is declared. This identifies that the variable is a pointer variable. The general form of the pointer variable declaration is
data _ type *pvar;
where pvar is the name of the pointer variable, data _ type refers to the data type of the pointer’s object. For Example,
float f;
float * fptr;
fptr = &f;
The first line declares f is a floating – point variable. The second line declares fptr is a pointer variable whose data item is a floating – point quantity. The third line assigns the address of the variable. fptr
Pointer variables can point to any ordinary type variables, arrays, functions, or other pointer variables. Thus
Once a pointer has been assigned the address of a variable, then the value of the variable can be accessed using unary operator (*) through the pointer variable. This operator usually known as indirection operator.
About the Author
Silan Software is one of the India's leading provider of offline & online training for Java, Python, AI (Machine Learning, Deep Learning), Data Science, Software Development & many more emerging Technologies.
We provide Academic Training || Industrial Training || Corporate Training || Internship || Java || Python || AI using Python || Data Science etc