Friday, 7 August 2015

DS: Pointers


POINTERS
Memorey is devided into number of cells. These cells are associated with fixed address. These addresses are called pointers.

Pointer concept can be explained using following terms...
1. Pointer constants
2.Pointer values
3.Pointer variable

Pointer constants: When we declare a variable of some type, memorey is allocated for that. Usually the first address with which it is identified is called Pointer constant. Fig A

Pointer values: When we declare a variable of some type, memorey is allocated for that, usually with the first address it is identified and remaining address are not important as pointer constant.
Fig A

Pointer variable: A variable which holds the address of another variable or a memorey location is called pointer variable. Fig B


Write a program to store some value into a variable using a pointer and display it?
#include<stdio.h>
void main()
{
int a;
int *p;
p=&a;
*p=10;
printf(“%d”,*p);
}
or
void main()
{
int a;
int *p=&a;
*p=10;
printf(“%d”,*p);
}

Operations performed on pointers;
Write a program to show thae various operations that can be performed on pointers?
#include<stdio.h>
void main()
{
int a=10,b=20;
int *p=&a, *q=&b;
int x=*p+*q;
int y=*p - *q;
int z= *p * *q;
int m=*q / *p;
printf(“addition=%d”, x);
printf(“substraction=%d”,y);
printf(“multiplication=%d”,z);
printf(“division=%d”,m);
}

NOTE:
let p and q be two pointers:
p-i valid
p+i valid
p*i invalid
p/i invalid

p-q valid
p+q invalid
p*q invalid
p/q invalid

NULL Pointer:
Pointer which points to NULL. Or it is a pointer which doesn't hold any address in it.

Single pointer: A pointer which points to a address(0*500), which address will hold the actual data.
int *p;
p=&a;





No comments:

Post a Comment

Total Pageviews