~lecture15.pptx - Todays lecture outline A program to...

Info icon This preview shows page 1. Sign up to view the full content.

This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Today’s lecture outline • • • • • A program to calculate percentage marks Array initialization Bound checking Passing array element to a function Pointer arithmetic 1 Marks program 2 Array initialization 3 Bounds Checking • In C there is no check to see if the subscript used for an array exceeds the size of the array • Data entered with a subscript exceeding the array size will simply be placed in memory outside the array 0 1 2 3 4 5 6 7 8 9 500 504 508 512 516 520 524 528 532 530 • To see to it that we do not reach beyond the array size is entirely the programmer’s botheration and not the compiler’s 4 Passing Array Elements to a Function • Array elements can be passed to a function by calling the function by value, or by address • In the call by value we pass values of array elements to the function • In the call by reference we pass addresses of array elements to the function 5 Value of array element pass to a function 0 1 2 3 4 5 6 55 65 75 56 78 78 90 500 504 508 512 516 520 524 ii ==406 2 3 1 5 7 display(marks[6]) display(marks[2]) display(marks[5]) display(marks[3]) display(marks[4]) display(marks[o]) display(marks[1]) display(90) display(75) display(78) display(56) display(65) Program output display(55) 55 65 75 56 78 78 90 6 Address of array element pass to a function 0 1 2 3 4 5 6 55 65 75 56 78 78 90 500 504 508 512 516 520 524 ii ==406 2 3 1 5 7 Program output 55 65 75 56 78 78 90 display(&marks[4]) display(&marks[2]) display(&marks[3]) display(&marks[1]) display(&marks[5]) display(&marks[6]) display(&marks[o]) display(508) display(516) display(512) display(504) display(520) display(524) display(500) *n *n 524 512 520 516 504 508 500 *(512) 56 *(520) *(524) *(516) 78 *(504) = 90 65 *(508) 75 *(500) 55 7 Pointer arithmetic • Addition of a number to a pointer *j 500 504 *j 10 j 500 0 10 500 *k 516 1 2 3 4 5 20 30 40 50 60 504 508 512 516 520 8 Cont. • Subtraction of a number from a pointer *k 504 0 10 500 *j 516 520 1 2 3 4 5 20 30 40 50 60 504 508 512 516 520 9 Points to remember • Array elements are always stored in contiguous memory locations. • A pointer when incremented always points to an immediately next location of its type • A pointer when decremented always points to the immediate previous location of its type • Comparison between pointers can test for either equality or inequality 10 ...
View Full Document

  • Summer '15

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern