Pointers in C

Pointers in C programming are used to point the locations in memory. A pointer is a variable whose value is the address of another variable, i.e., the direct address of the memory location.

Pointers

Pointers are aptly name: they 'point' to locations in memory. 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 you can use it to store any variable address. The general form of a pointer variable declaration is: type *var-name; Here, type is the pointer's base type; it must be a valid C data type and 'var-name' is the name of the pointer variable. The asterisk * you used to declare a pointer is the same asterisk that you use for multiplication. However, in this statement the asterisk is being used to designate a variable as a pointer.

Following are the valid pointer declaration:
int *ip; pointer to an integer
double *dp; pointer to a double
float *fp; pointer to a float
char *ch; pointer to a character

The actual data type of the value of all pointers, whether integer, float, character, or otherwise, is the same, a long hexadecimal number that represents a memory address. The only difference between pointers of different data types is the data type of the variable or constant that the pointer points to.


Program Using Pointer

Program

#include<stdio.h>
#include<conio.h>
void main()
{
int x; //A normal integer.
int *p; //A pointer to an integer, '*p' is an integer, so 'p'
            //must be a pointer to an integer.
p = &x; //Read it, 'assign the address of 'x' to 'p'.
scanf("%d", &x); //Put a value in 'x', we could also use 'p' here.
printf("%d\n", *p); //Note the use of the '*' to get the value.
getch();
}
Output
F
68886948

Program NULL Pointer

Program

#include<stdio.h>
#include<conio.h>
void main()
{
int *ptr = NULL; //assigning Null value to 'ptr'.
printf("The value of ptr is : %x\n", ptr); //printing 'ptr' value.
getch();
}
Output
The value of ptr is : 0