Pointers in C

http://install4install.com

Pointer is a variable which stores address of another variable(memory location which has certain value).When we declare a variable ,a specific memory is allocated to the variable depending on the data type.Consider the following statements :

int age = 25; //Fig 1

Fig 1: Allocation of integer variable

float percentage = 90.6; //Fig 2
Fig 2 : Allocation of float variable.

Fig 2 : Allocation of float variable

A specific memory location(Say 200) is allocated to variable age(Fig 1).2 bytes will be given as it is of int type and the value 25 will be stored and 4 bytes will be allocated to percentage (Fig 2) as it of float type at say 300 location.

In order to store these locations/addresses we use pointers.

Declaration of Pointers

datatype *pointer_name;

Initialization of Pointers

datatype variable_name = value;

datatype  *pointer_name = &variable_name;

Example 1

#include<stdio.h>
int main()
{
int age =25;
int *ptr =&age;
printf("The value of variable is %d\n",age);
printf("The address of variable is %d \n",ptr);
printf("The value of variable is %d",*ptr);
}

Run

 

 

Example 2

Multiplication,Addition and Division using Pointers

#include <stdio.h>
int main()
{
	int a,b;
	int sum =0,mul=1,div=1;
	int *ptr1,*ptr2;
	ptr1 = &a;
	ptr2= &b;
	//printf("Enter two numbers\n");
	scanf("%d%d",&a,&b);
	sum = *ptr1 + *ptr2;
	mul = *ptr1 * *ptr2;
	div = *ptr1/ *ptr2;
	printf("The sum is %d\n",sum);
	printf("The multiplication is %d\n",mul);
	printf("The division is %d\n",div);
	return 0;

}

Run

TipWe can add integers to or subtract integers from pointers as well as subtract one pointer from the other.
TipWe can compare pointers by using relational operators in the expressions. For e.g. p1>p2,p1==p2 and p1!=p2 are all valid in C.

Generic Pointers

These are the pointer variables of void as the data type.We need to type cast these pointers as we cannot use void as the data type directly with the variable.They are essential when we want a pointer to point to data of different data types at different times.A single generic pointer is used to store the address  of int,float or char types at different times.

Program

#include<stdio.h>
int main()
{
int age=10;
char name='c';
void *ptr;
ptr=&age;
int typecast1 = *(int*)ptr;
printf("Generic Pointer points to integer value %d\n",typecast1);
ptr = &name;
char typecast2 = *(char*)ptr;
printf("Generic Pointer points to character name %c",typecast2);
return 0;
}

Run

Illustration of Program

  • ptr is a generic pointer (void as the data type).
  • ptr stores the address of variable age.
  • typecast1 variable of int type is used  to store *(int*)ptr  to convert  void to int type and is printed using printf.
  • Now ptr stores the address of variable name which is of char type.
  • typecast2 variable of char type is used to convert void  to char type  and is printed on screen using printf.
TipIf typecasting is not done and pointer value is printed as such(*ptr) then error message would be displayed on screen.

Null Pointers

Screen Shot 2014-04-07 at 11.51.23 AM

Fig 3 : Null Pointers

Null Pointers does not point to any valid memory address(Fig 3).In order to declare a null pointer,a predefined constant NULL is used.They are  used to represent conditions such as the end of a list of unknown length or the  failure  to perform certain action.

 

Syntax

int *ptr = NULL;

 

 







Short URL: http://tinyit.cc/10e8b
Author: Cusp2207 on March 31, 2014
Category: Computer Science
Tags:

Leave a Reply

Last articles