Академический Документы
Профессиональный Документы
Культура Документы
5 Pointers
Pointer
0
1
2
3
.
.
.
.
.
.
.
65,535
Memory Organization
Pointer
Quantity Variable
126 Value
5000 Address
Representation of a Variable
5048
P 5000
Pointer Variable
Pointer
Pointer
Example:
void main( )
{
int a;
char d;
float k;
a = 10;
d = ‘J’;
k = 15.23;
printf(“Address of %d is %u”, a, &a);
printf(“Address of %c is %u”, d, &d);
printf(“Address of %f is %u”, k, &k);
data_type *ptr_name;
Example:
P ? ?
Contains Points to
Garbage Unknown location
Multiple Declarations:
Example:
int quantity;
int *p; /*Declaration*/
p = &quantity; /*Initialization*/
or
int *p = &quantity;
Pointers Flexibility
int x, y, z, *p;
...... x y z
p = &x;
......
p = &y;
......
p = &z;
...... p
int x; p1 p2 p3
int *p1 = &x;
int *p2 = &x;
int *p3 = &x; x
.........
.........
* - Indirection operator or
Dereferencing operator.
Example:
n = *&marks;
Chain of Pointers
p2 p1 Variable
Multiple Indirections
void main( )
{
int x, *p1, **p2;
x = 100;
p1 = &x;
p2 = &p1;
printf(“Value : %d”,**p2);
}
Pointer Expressions
y = *p1 * *p2;
sum = sum + *p3;
z = 5 - *p2/ *p1;
*p2 = *p1 + 10;
p1++;
-p2;
sum += *p2;
Elements
X[0] X[1] X[2] X[3] X[4]
Value 8 4 9 6 3
1000 1002 1004 1006 1008
Address
#include<stdio.h>
void main( )
{
int *p, i;
int array[5] = {8, 9, 6, 3, 2};
p = array;
for(i=0; i<5; i++)
{
printf(“\n %d”,*p); //1
p++; //2
}
}
1 printf(“\n %d”,*(p+i));