Pointers

Pointer is a variable that holds address that is memory location of variable rather than actual value. Pointer is a variable that points to reference a memory location where data is stored. It provides powerful and flexible method for mapping data in programs. Pointers are used in C, as they have numbers of useful applications. Pointers provides a way to return multiple data items from a function via function arguments.

Features of Pointers:

  1. A pointer enables us to access a variable that is defined outside the function.
  2. Pointers are used to pass array to functions.
  3. Pointers are used in dynamic memory location.
  4. Pointers produce compact, efficient, and powerful code with high execution speed.
  5. Pointers are more efficient in handling data table.
  6. With the help of pointer, variables can be swapped without physically moving them.

Declaration of Pointer Variables
Like other variables pointer variables must be declared before they may be used. Asterisk (*) is used to define pointer variables.

Syntax:        data_type *pointer_name;
Examples:    int *num; float *salary; char *c

Note: Pointer can hold the address of variables of same data types only.

#include<stdio.h>
#include<conio.h>
void main(){
	int x=9, *ptr;
	ptr=&x;
	printf("Value of x =%d\n",x);
	printf("Value of x =%d\n",*ptr);
	printf("Address of x= %d\n",ptr);
	printf("Address of x= %d",&x);
	x=11;
	printf("Value of x =%d\n",x);
	printf("Value of x =%d\n",*ptr);
	printf("Address of x= %d\n",ptr);
	printf("Address of x= %d",&x);
	getch();
}
#include<stdio.h>
#include<conio.h>
void main(){
	int x=9,y=10,s, *ptr1,*ptr2,*ptr3;
	ptr1=&x;
	ptr2=&y;
	ptr3=&s;
	s=x+y;
	printf("Value of x =%d\n",x);
	printf("Value of y =%d\n",y);
	printf("Sum =%d\n",s);
	*ptr1=20;
	*ptr2=30;
	printf("Value of x =%d\n",x);
	printf("Value of y =%d\n",y);
	s=*ptr1+*ptr2;
	printf("Sum =%d\n",s);
	printf("Sum =%d\n",*ptr3);
	printf("Address of sum [s]= %d",ptr3);
	getch();
}

Leave a Reply