Вы находитесь на странице: 1из 12

Chapter no.

5 Pointers

Pointer

Memory Cell Address

0
1
2
3
.
.
.
.
.
.
.
65,535

Memory Organization

-1- By, Kute T. B. for CPR (FYIF) 2007-08


Chapter no. 5 Pointers

Pointer

int quantity = 126;

Quantity Variable

126 Value

5000 Address

Representation of a Variable

Variable Value Address

Quantity 126 5000

5048
P 5000

Pointer Variable

-2- By, Kute T. B. for CPR (FYIF) 2007-08


Chapter no. 5 Pointers

Pointer

Pointer Pointer Pointer


Constants Values Variables

Pointer

Pointer Constants: Memory addresses


within a computer.

Pointer Value: Value stored at the address.

Pointer Variable: Pointer value can be


stored in another variable.

-3- By, Kute T. B. for CPR (FYIF) 2007-08


Chapter no. 5 Pointers

Accessing address of the variable

& - ‘Address of’ operator.

This unary operator is used to obtain


address of a variable and can only be used
with single variable or single array
element. It can not be used with constants,
whole array or expressions.

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);

-4- By, Kute T. B. for CPR (FYIF) 2007-08


Chapter no. 5 Pointers

Declaring pointer variables

data_type *ptr_name;

This tells compiler three things about the


variable ptr_name:

1. * tells that the variable ptr_name is


a pointer variable.
2. ptr_name needs a memory location.
3. ptr_name points to a variable of
type data_type.

Example:

int *p; /*integer pointer */

float *x; /* float pointer */

P ? ?
Contains Points to
Garbage Unknown location

-5- By, Kute T. B. for CPR (FYIF) 2007-08


Chapter no. 5 Pointers

Pointer Declaration Style

int* p; /* Style 1*/


int *p; /* Style 2*/
int * p; /* Style 3*/

Multiple Declarations:

int *p, x, *q;

Initialization of Pointer Variables

Example:

int quantity;
int *p; /*Declaration*/
p = &quantity; /*Initialization*/

or

int *p = &quantity;

-6- By, Kute T. B. for CPR (FYIF) 2007-08


Chapter no. 5 Pointers

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
.........
.........

-7- By, Kute T. B. for CPR (FYIF) 2007-08


Chapter no. 5 Pointers

Accessing variable through pointer

* - Indirection operator or
Dereferencing operator.

We can call it ‘Contents of’.

Example:

int marks, *p, n;


marks = 79;
p = &marks;
n = *p;
-------------------------------
p = &marks;
n = *p;
Equivalent to:

n = *&marks;

-8- By, Kute T. B. for CPR (FYIF) 2007-08


Chapter no. 5 Pointers

Chain of Pointers

Address2 Address1 Value

p2 p1 Variable

Multiple Indirections

void main( )
{
int x, *p1, **p2;
x = 100;
p1 = &x;
p2 = &p1;
printf(“Value : %d”,**p2);
}

-9- By, Kute T. B. for CPR (FYIF) 2007-08


Chapter no. 5 Pointers

Pointer Expressions
y = *p1 * *p2;
sum = sum + *p3;
z = 5 - *p2/ *p1;
*p2 = *p1 + 10;

p1++;
-p2;
sum += *p2;

We may not use pointers in


Division / Multiplication
i.e. p1 / p2 or p1 * p2 or p1 / 3

Similarly, two pointers can not be


added. i.e. p1 + p2 is not allowed.

- 10 - By, Kute T. B. for CPR (FYIF) 2007-08


Chapter no. 5 Pointers

Pointers and Arrays

int x[5] = {8, 4, 9, 6, 3};

Elements
X[0] X[1] X[2] X[3] X[4]

Value 8 4 9 6 3
1000 1002 1004 1006 1008
Address

Assigning the array to pointer:


int *p;
p = x; or p = &x[0];

Now, we can access every value of


x using p++ to move from one
element to another.

- 11 - By, Kute T. B. for CPR (FYIF) 2007-08


Chapter no. 5 Pointers

Pointers and Arrays

#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));

- 12 - By, Kute T. B. for CPR (FYIF) 2007-08

Вам также может понравиться