{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Lecture8

Lecture8 - ECE15 Introduction to Computer Programming Using...

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

ECE15: Introduction to Computer Programming Using the C Language Lecture Unit 8: Pointers

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

View Full Document
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
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

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

View Full Document
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?
#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.

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

View Full Document
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
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

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

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

{[ snackBarMessage ]}

What students are saying

• 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.

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

• 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.

Dana University of Pennsylvania ‘17, Course Hero Intern

• 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.

Jill Tulane University ‘16, Course Hero Intern