Pointer variables declaration and Initialization

Pointer variables declaration: Pointer variables are used to store the address of the variable. And a pointer variable can store only the address of the variable which has the same data-type as the pointer variable.

Syntax:            data-type  *pointer-variable name;

e.g:      int *ptr;

In the above example we have declare the pointer variable with the name of ‘ptr’ and its data-type in int, that means it can store the address of the variable which is of integer type.

 Initialization of pointer variables: The initialization of the pointer variable is simple like other variable but in the pointer variable we assign the address instead of value.

e.g:      int  *ptr,var1;

ptr=&var1;

Here in the above example we have a pointer variable and another is a simple integer variable and we have assign the pointer variable with the address of the ‘var1’. That means the pointer variable ‘ptr’ is now has the address of the variable ‘var1’.

If we want to access the pointer variable then we have to understand the most important thing which is as follows:

If the ‘*’ asterisk sign is before the pointer variable this means the pointer variable is now pointing to the value at the location instead of the pointing to location.

Program:

PROGRAM TO FIND REVERSE OF A NUMBER

#include<stdio.h>

#include<conio.h>

void main()

{

int n,a;

int *rev,*rem,*temp;

clrscr();

printf(“Enter any number: “);

scanf(“%d”,&n);

a=n;

temp=&n;

*rev=0;

while(*temp>0)

{

*rem=*temp%10;

*temp=*temp/10;

*rev=(*rev)*10+*rem;

}

printf(“Reverse of %d is %d”,a,*rev);

getch();

}

Posted in C

Leave a Reply

Your email address will not be published. Required fields are marked *