18
Lecture 15 Lecture 15 Version 1.0 Version 1.0 Pointers and Arrays Pointers and Arrays

C-15

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: C-15

Lecture 15Lecture 15Version 1.0Version 1.0

Pointers and ArraysPointers and Arrays

Page 2: C-15

2Rushdi Shams, Dept of CSE, KUET, Bangladesh

Passing Array Elements to Passing Array Elements to FunctionsFunctions

Array elements can be passed to a Array elements can be passed to a function by calling the function by function by calling the function by value, or by reference value, or by reference

In the call by value we pass values of In the call by value we pass values of array elements to the function array elements to the function

in the call by reference we pass in the call by reference we pass addresses of array elements to the addresses of array elements to the function function

Page 3: C-15

3Rushdi Shams, Dept of CSE, KUET, Bangladesh

Passing Array Elements to Passing Array Elements to Functions: Call by valueFunctions: Call by value

Page 4: C-15

4Rushdi Shams, Dept of CSE, KUET, Bangladesh

Passing Array Elements to Passing Array Elements to Functions: Call by referenceFunctions: Call by reference

Page 5: C-15

5Rushdi Shams, Dept of CSE, KUET, Bangladesh

Pointer ArithmeticPointer Arithmetic

Page 6: C-15

6Rushdi Shams, Dept of CSE, KUET, Bangladesh

Pointer ArithmeticPointer Arithmetic

Addition of a number to a pointer Addition of a number to a pointer

Page 7: C-15

7Rushdi Shams, Dept of CSE, KUET, Bangladesh

Pointer ArithmeticPointer Arithmetic

Subtraction of a number from a pointer Subtraction of a number from a pointer

Page 8: C-15

8Rushdi Shams, Dept of CSE, KUET, Bangladesh

What do we know so far?What do we know so far?

Array elements are always stored in Array elements are always stored in contiguous memory locations. contiguous memory locations.

A pointer when incremented always A pointer when incremented always points to an immediately next points to an immediately next location of its type. location of its type.

Page 9: C-15

9Rushdi Shams, Dept of CSE, KUET, Bangladesh

Pointers and ArraysPointers and Arrays

Page 10: C-15

10Rushdi Shams, Dept of CSE, KUET, Bangladesh

Pointer and ArrayPointer and Array

Our next two programs show ways in Our next two programs show ways in which we can access the elements of which we can access the elements of this array. this array.

Page 11: C-15

11Rushdi Shams, Dept of CSE, KUET, Bangladesh

Pointer and ArrayPointer and Array

Page 12: C-15

12Rushdi Shams, Dept of CSE, KUET, Bangladesh

Pointer and ArrayPointer and Array

Page 13: C-15

13Rushdi Shams, Dept of CSE, KUET, Bangladesh

Passing entire array to Passing entire array to functionfunction

Page 14: C-15

14Rushdi Shams, Dept of CSE, KUET, Bangladesh

Similar Statements!Similar Statements!

Page 15: C-15

15Rushdi Shams, Dept of CSE, KUET, Bangladesh

Array of PointersArray of Pointers

The way there can be an array of The way there can be an array of intints or an array of s or an array of floatfloats, similarly s, similarly there can be an array of pointers there can be an array of pointers

an array of pointers would be an array of pointers would be nothing but a collection of addresses nothing but a collection of addresses

Page 16: C-15

16Rushdi Shams, Dept of CSE, KUET, Bangladesh

Array of pointersArray of pointers

Page 17: C-15

17Rushdi Shams, Dept of CSE, KUET, Bangladesh

Array of pointersArray of pointers

Page 18: C-15

18Rushdi Shams, Dept of CSE, KUET, Bangladesh

Pointers to arrayPointers to array

An array of pointers can even contain An array of pointers can even contain the addresses of other arrays the addresses of other arrays