{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Lecture8 - ECE15 Introduction to Computer Programming Using...

Info icon This preview shows pages 1–9. Sign up to view the full content.

View Full Document Right Arrow Icon
ECE15: Introduction to Computer Programming Using the C Language Lecture Unit 8: Pointers
Image of page 1

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Why Do We Need Pointers? Pointers The address & and indirection * operators Pointers and functions: call by reference The NULL constant and the void * type Lecture Unit 8 ECE15: Introduction to Computer Programming Using the C Language 2 Lecture Outline Pointer Arithmetic in C Arrays and Pointers
Image of page 2
4 3 The Curse of Calling by Value Lecture Unit 8 ECE15: Introduction to Computer Programming Using the C Language 3 x y x y 4 3 temp 3 What to do? Recall that in C, arguments are passed to functions by value . When a function f( int y) is called as f(x) , the current value of the vari- able x is sampled and copied into y . The function has no access to x . void swap( int x, int y) { int temp = x; x = y; y = temp; } int main( void ) { int x = 3 , y = 4 ; swap(x,y); ... } Example: We often need to swap the values o of two variables x y . Write a function for this. Passing variables by value
Image of page 3

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Problems with Arrays Lecture Unit 8 ECE15: Introduction to Computer Programming Using the C Language 4 double average( int b[], int N); int main( void ) { double avg; int a[ 1000000 ] = { 1 , 2 ,...}; avg = average(a, 1000000 ); ... } double average( int b[], int N) { int i, sum = 0 ; for (i = 0 ; i < N; i++) sum += b[i]; return sum /( double ) N; } Will the program copy the 1,000,000 integers from a[] to b[] ? Average of a large array: Array of unknown size? #include <stdio.h> int main( void ) { int i, N; scanf( "%d" ,&N); int a[N] = { 0 }; for (i = 0 ; i < N; i++) scanf( "%d" ,&a[i]); ... } What to do?
Image of page 4
#include <stdio.h> double quad_solve( int , int , int ); int main( void ) { double a = 1.0 , b = 4.0 , c = 1.0 ; double x[ 2 ]; x = quad_solve(a,b,c); printf( "The roots are %d and %d\n" , x[0],x[1]); return 0 ; } double quad_solve( int a, int b, int c) { double root[ 2 ]; ... root[1] = ... ; root[2] = ... ; return root; } #include <stdio.h> double quad_solve( int main( void ) { double a = 1.0 , b = 4.0 , c = 1.0 ; double x[ 2 ]; x = quad_solve(a,b,c); printf( "The roots are %d and %d\n" , x[0],x[1]); return 0 ; } double quad_solve( int a, int b, int c) { double root[ 2 ]; ... root[1] = ... ; root[2] = ... ; return root[1], root[2]; } Functions Returning Two Values? Lecture Unit 8 ECE15: Introduction to Computer Programming Using the C Language 5 Example: Write a function that computes the roots of a general o quadratic equation ax 2 + bx + c = 0 . Print these roots.
Image of page 5

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Solution to All the Problems Lecture Unit 8 ECE15: Introduction to Computer Programming Using the C Language We have observed several fundamental problems in C: A function cannot change the variables passed to it by value Passing arrays to a function by value wastes a lot of memory and time Cannot handle arrays whose size is unknown at compilation time Functions cannot return to the calling environment more than one value 6 Instead of using the variables themselves, use their addresses! The same idea solves all these problems: Pass by value Pass by reference
Image of page 6
Why Do We Need Pointers? Pointers The address & and indirection * operators Pointers and functions: call by reference The NULL constant and the void * type Lecture Unit 8 ECE15: Introduction to Computer Programming Using the C Language 7 Lecture Outline Pointer Arithmetic in C Arrays and Pointers
Image of page 7

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
... ...
Image of page 8
Image of page 9
This is the end of the preview. Sign up to access the rest of the document.

{[ 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