Cpr Transperencies - Ch 05 Pointers

  • November 2019
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View Cpr Transperencies - Ch 05 Pointers as PDF for free.

More details

  • Words: 706
  • Pages: 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; Variable

Quantity

Value

126 5000

Address

Representation of a Variable

Variable

Value

Quantity

126

P

Address 5000

5048

5000

Pointer Variable

-2-

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

Chapter no. 5 Pointers

Pointer

Pointer Constants

Pointer Values

Pointer 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 Garbage

Points to Unknown location -5-

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

Chapter no. 5 Pointers

Pointer Declaration Style int* p; int *p; int * p;

/* Style 1*/ /* Style 2*/ /* Style 3*/

Multiple Declarations: int *p, x, *q; Initialization of Pointer Variables Example: int quantity; int *p; p = &quantity;

/*Declaration*/ /*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; ...... p = &x; ...... p = &y; ...... p = &z; ......

int x;

x

y

z

p

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

p2

Address1

p1

Value

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

Value Address

X[0]

X[1]

X[2]

X[3]

8

4

9

6

3

1000

1002

1004

1006

1008

X[4]

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

Related Documents