C-15

Post on 19-Jan-2015

89 views 0 download

Tags:

description

 

Transcript of C-15

Lecture 15Lecture 15Version 1.0Version 1.0

Pointers and ArraysPointers and Arrays

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

3Rushdi Shams, Dept of CSE, KUET, Bangladesh

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

4Rushdi Shams, Dept of CSE, KUET, Bangladesh

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

5Rushdi Shams, Dept of CSE, KUET, Bangladesh

Pointer ArithmeticPointer Arithmetic

6Rushdi Shams, Dept of CSE, KUET, Bangladesh

Pointer ArithmeticPointer Arithmetic

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

7Rushdi Shams, Dept of CSE, KUET, Bangladesh

Pointer ArithmeticPointer Arithmetic

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

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.

9Rushdi Shams, Dept of CSE, KUET, Bangladesh

Pointers and ArraysPointers and Arrays

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.

11Rushdi Shams, Dept of CSE, KUET, Bangladesh

Pointer and ArrayPointer and Array

12Rushdi Shams, Dept of CSE, KUET, Bangladesh

Pointer and ArrayPointer and Array

13Rushdi Shams, Dept of CSE, KUET, Bangladesh

Passing entire array to Passing entire array to functionfunction

14Rushdi Shams, Dept of CSE, KUET, Bangladesh

Similar Statements!Similar Statements!

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

16Rushdi Shams, Dept of CSE, KUET, Bangladesh

Array of pointersArray of pointers

17Rushdi Shams, Dept of CSE, KUET, Bangladesh

Array of pointersArray of pointers

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