Lecture8

# Lecture8 - ECE15: Introduction to Computer Programming...

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( 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 }; 0 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.

## This note was uploaded on 04/15/2010 for the course ECE ECE15 taught by Professor Vardy during the Fall '08 term at UCSD.

### Page1 / 34

Lecture8 - ECE15: Introduction to Computer Programming...

This preview shows document pages 1 - 9. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online