Presentation Transcript
Slide 1:Pointers
Pointers:
Pointer is nothing but a variable that contains an address which is a location of another variable in memory.
Understanding Pointers:
Computers use their memory for storing instruction of a program as well as value of variables associated with it.
The computer memory is a sequential collection of storage cells. Each cell is commonly known as bytes, has a number called address.
The address is a numbered consecutively from O. The last address depends on the memory size.
Whenever we declare a variable the system allocates appropriate location to hold the value of the variable.
Slide 2:Consider the following statement:
int i=170;
This statement instruct the system to find the location for the integer variable i and puts a value 170 in that location.
Letters assume that system has the address location 5000 for i.
During execution of the program the system associates the name i with the address 5000. If we have access to value 170 by using either the name i or the address 5000.
The memory address are simply numbers, they can be assigned to some variables which can stored in memory, like any other variable. Such variables that hold memory addresses are called pointers.
Variable Value Address
i 170 5000
p 5000 5048
Slide 3:Accessing the address of a variable
The actual location of a variable is system depended and the address is not known to us immediately. This can be done with the help of & operator available in C.
The operator & preceding a variable returns the address of a variable associated with it.
P = &i;
would assign the address 5000 to the variable P.
Slide 4:Program:
#include
main()
{
char a;
int x;
float p ,q;
a=’A’;
x=152;
p=10.25;
q=18.76;
printf (“%c is stored at addr %u\n”, a, &s);
printf (“%d is stored at addr %u\n”, x, &x);
printf (“%f is stored at addr %u\n”, p, &p);
printf (“%f is stored at addr %u\n”, q, &q);
}
Slide 5:#include
#include
int main()
{
double radius=2.0,area;
double *pradius, *parea;
pradius=&radius;
parea=&area;
*parea= 3.14 * (*pradius) * (*pradius);
printf("%f", *parea);
getch();
} Program: