270 likes | 386 Views
16.216 ECE Application Programming. Instructor: Dr. Michael Geiger Fall 2011 Lecture 22: Pointers. Lecture outline. Announcements/reminders Coming up: PE4 on Monday Program 6 to be posted Monday, due Monday 11/7
E N D
16.216ECE Application Programming Instructor: Dr. Michael Geiger Fall 2011 Lecture 22: Pointers
Lecture outline • Announcements/reminders • Coming up: • PE4 on Monday • Program 6 to be posted Monday, due Monday 11/7 • Program 5 grading to be done this weekend; regrades due by Tuesday 11/1 • Exam 2: Wednesday 11/9 • Midterm grades posted only for those at risk • FN—Failing (never attended) • IDF—In danger of failing • Today • Pointers, pointer arguments ECE Application Programming: Lecture 22
Review: functions • Used to break programs into smaller pieces • Useful when code sequences repeated • Functions have: • An optional return value • Return type is always required • If function returns nothing, return type is void • A name • Optional arguments • Must be prototyped or written completely prior to use ECE Application Programming: Lecture 22
Example 1 • What does the following print? int f(int a, int b); int main() { int x = 1; int y = 2; int result1, result2, result3; result1 = f(x, y); result2 = f(y, result1); result3 = f(result1, result2); printf("x = %d, y = %d\n", x, y); printf("Result 1: %d\n", result1); printf("Result 2: %d\n", result2); printf("Result 3: %d\n", result3); return 0; } int f(int a, int b) { int i; // Loop index int r = 0; // Result for (i = 0; i < a; i++) r += b; return r; } ECE Application Programming: Lecture 22
Example 1 solution x = 1, y = 2 Result 1: 2 Result 2: 4 Result 3: 8 ECE Application Programming: Lecture 22
Example 2: Functions • Write a function that: • Prints a series of LINE_LENGTH dashes on a single line • LINE_LENGTH is a predefined constant (using #define) • Reads an integer value from the console input and returns 1 if the value is even, 0 if it’s odd • Takes four double-precision numbers as arguments and returns their average ECE Application Programming: Lecture 22
Example 2 solutions • Write a function that: prints a series of LINE_LENGTH dashes on a single line • LINE_LENGTH is a predefined constant (using #define) void printLine() { inti; for (i = 0; i < LINE_LENGTH; i++) printf(“-”); } ECE Application Programming: Lecture 22
Example 2 solutions (cont.) • Write a function that: reads an integer value from the console input and returns 1 if the value is even, 0 if it’s odd intcheckEven() { int value; printf(“Enter integer value: ”); scanf(“%d”, &value); if ((value % 2) == 0) return 1; else return 0; } ECE Application Programming: Lecture 22
Example 2 solutions (cont) • Write a function that: takes four double-precision numbers as arguments and returns their average double avgFour(double a, double b, double c, double d) { return (a + b + c + d) / 4.0; } ECE Application Programming: Lecture 22
Justifying pass by address • May want the ability to “return” multiple values from function • Functions can only return at most one value • Functions can take multiple arguments ... • ... but, as we’ve discussed so far, passing by value just copies arguments • No way to change arguments and have change reflected outside of function • Solution uses pointers ECE Application Programming: Lecture 22
Pointers • Pointer: address of a variable • Can get address of existing object using & • Can get value of existing pointer using * • Pointer declaration: <base type>* <pointer name> • Base type determines how reference is interpreted • Be careful when declaring multiple pointers • What types do p1, p2, and p3 have below? • int *p1, p2, p3; • Be sure to initialize pointer before use • Always good to set pointer = NULL until it’s used • *p doesn’t make sense if you don’t know where p points! ECE Application Programming: Lecture 22
Basic pointer example int *iPtr, i=6; double *dPtr, d=1.25; iPtr dPtr i d 6 1.25 ECE Application Programming: Lecture 22
x xp ip Pointer assignment • The assignment operator (=) is defined for pointers of the same base type. • The right operand of the assignment operator can be any expression that evaluates to the same type as the left operand. • Example: int x, *xp, *ip; xp = &x; ip = xp; ECE Application Programming: Lecture 22
x xp ip Dereferencing pointers • Dereference: access variable using pointer • Use the * operator • Expanding previous example: int x, *xp, *ip; xp = &x; ip = xp; *xp = 7; // x == 7 *ip = 18; // x == 18 ECE Application Programming: Lecture 22
Example: using pointers • What does the following print? void main() { int x = 7, y = 10; int *xp = &x; int *yp = &y; int *p; int i; for (i = 0; i < 5; i++) { (*xp)++; (*yp)--; if ((i % 3) == 0) p = xp; else p = yp; *p = *xp + *yp; } printf("x = %d, y = %d\n", x, y); } ECE Application Programming: Lecture 22
Example solution Walk through all 5 loop iterations: First iteration (i == 0): (*xp)++ x++ x = 7+1 = 8 (*yp)-- y-- y = 10-1 = 9 (i % 3) == 0 p = xp *p = *xp + *yp x = x + y = 8 + 9 = 17 Second iteration (i == 1): (*xp)++ x++ x = 17+1 = 18 (*yp)-- y-- y = 9-1 = 8 (i % 3) != 0 p = yp *p = *xp + *yp y = x + y = 18 + 8 = 26 ECE Application Programming: Lecture 22
Example solution (cont) Walk through all 5 loop iterations: Third iteration (i == 2): (*xp)++ x++ x = 18+1 = 19 (*yp)-- y-- y = 26-1 = 25 (i % 3) != 0 p = yp *p = *xp + *yp y = x + y = 25 + 19 = 44 Fourth iteration (i == 3): (*xp)++ x++ x = 19+1 = 20 (*yp)-- y-- y = 44-1 = 43 (i % 3) == 0 p = xp *p = *xp + *yp x = x + y = 20 + 43 = 63 ECE Application Programming: Lecture 22
Example solution (cont) Walk through all 5 loop iterations: Fifth iteration (i == 4): (*xp)++ x++ x = 63+1 = 64 (*yp)-- y-- y = 43-1= 42 (i % 3) != 0 p = yp *p = *xp + *yp y = x + y = 64 + 42 = 106 Final output x = 64, y = 106 ECE Application Programming: Lecture 22
Pointer arguments • Passing pointer gives ability to modify data at that address • In prototype/definition—argument has pointer type • For example: int f(int *addr_x); • When calling function, can pass explicit pointer or use address operator (&<var>) • Examples: int x = 3; int y = 2; int *xPtr = &x; int result1, result2; result1 = f(xPtr); result2 = f(&y); ECE Application Programming: Lecture 22
Functions - pass by address #include <stdio.h>#include <math.h>void get_r_theta(double a, double b, double *adr_r, double *adr_th);void main(){ double x,y,h,r,th; printf("Enter x, y components of vector: "); scanf("%lf %lf",&x,&y); get_r_theta(x,y,&r,&th); printf("Vector with x=%lf and y=%lf has r=%lf, theta=%lf\n",x,y,r,th); } void get_r_theta(double a, double b, double *adr_r, double *adr_th) { double sum; sum = pow(a,2)+pow(b,2); //or a*a+b*b; *adr_r = sqrt(sum); *adr_th = atan2(y,x);} x 4600 y 4608 r 4610 th 4618 ECE Application Programming: Lecture 22
Functions - pass by address #include <stdio.h>#include <math.h>void get_r_theta(double a, double b, double *adr_r, double *adr_th);void main(){ double x,y,h; printf("Enter x, y components of vector: "); scanf("%lf %lf",&x,&y); // user enters 3,4 get_r_theta(x,y,&r,&th); printf("Vector with x=%lf and y=%lf has r=%lf, theta=%lf\n",x,y,r,th); } void get_r_theta(double a, double b, double *adr_r, double *adr_th) { double sum; sum = pow(a,2)+pow(b,2); //or a*a+b*b; *adr_r = sqrt(sum); *adr_th = atan2(y,x);} x 3.0 4600 y 4.0 4608 r ? 4610 th ? 4618 ECE Application Programming: Lecture 22
Functions - pass by address #include <stdio.h>#include <math.h>void get_r_theta(double a, double b, double *adr_r, double *adr_th);void main(){ double x,y,h; printf("Enter x, y components of vector: "); scanf("%lf %lf",&x,&y); // user enters 3,4 get_r_theta(x,y,&r,&th); printf("Vector with x=%lf and y=%lf has r=%lf, theta=%lf\n",x,y,r,th); } void get_r_theta(double a, double b, double *adr_r, double *adr_th) { double sum; sum = pow(a,2)+pow(b,2); //or a*a+b*b; *adr_r = sqrt(sum); *adr_th = atan2(b,a);} x 3.0 4600 y 4.0 4608 r 4610 th 4618 a 3.0 7380 b 4.0 7385 adr_r 4610 7388 adr_th 4618 738c sum 7380 ECE Application Programming: Lecture 22
Functions - pass by address #include <stdio.h>#include <math.h>void get_r_theta(double a, double b, double *adr_r, double *adr_th);void main(){ double x,y,h; printf("Enter x, y components of vector: "); scanf("%lf %lf",&x,&y); // user enters 3,4 get_r_theta(x,y,&r,&th); printf("Vector with x=%lf and y=%lf has r=%lf, theta=%lf\n",x,y,r,th); } void get_r_theta(double a, double b, double *adr_r, double *adr_th) { double sum; sum = pow(a,2)+pow(b,2); //or a*a+b*b; *adr_r = sqrt(sum); *adr_th = atan2(b,a);} x 3.0 4600 y 4.0 4608 r ? 4610 th ? 4618 a 3.0 7380 b 4.0 7385 adr_r 4610 7388 adr_th 4618 738c sum 25.0 7380 ECE Application Programming: Lecture 22
Functions - pass by address #include <stdio.h>#include <math.h>void get_r_theta(double a, double b, double *adr_r, double *adr_th);void main(){ double x,y,h; printf("Enter x, y components of vector: "); scanf("%lf %lf",&x,&y); // user enters 3,4 get_r_theta(x,y,&r,&th); printf("Vector with x=%lf and y=%lf has r=%lf, theta=%lf\n",x,y,r,th); } void get_r_theta(double a, double b, double *adr_r, double *adr_th) { double sum; sum = pow(a,2)+pow(b,2); //or a*a+b*b; *adr_r = sqrt(sum); *adr_th = atan2(b,a);} x 3.0 4600 y 4.0 4608 r 5.0 4610 th ? 4618 a 3.0 7380 b 4.0 7385 adr_r 4610 7388 adr_th 4618 738c sum 25.0 7380 ECE Application Programming: Lecture 22
Functions - pass by address #include <stdio.h>#include <math.h>void get_r_theta(double a, double b, double *adr_r, double *adr_th);void main(){ double x,y,h; printf("Enter x, y components of vector: "); scanf("%lf %lf",&x,&y); // user enters 3,4 get_r_theta(x,y,&r,&th); printf("Vector with x=%lf and y=%lf has r=%lf, theta=%lf\n",x,y,r,th); } void get_r_theta(double a, double b, double *adr_r, double *adr_th) { double sum; sum = pow(a,2)+pow(b,2); //or a*a+b*b; *adr_r = sqrt(sum); *adr_th = atan2(b,a);} x 3.0 4600 y 4.0 4608 r 5.0 4610 th 36.87 4618 a 3.0 7380 b 4.0 7388 adr_r 4610 7390 adr_th 4618 7394 sum 25.0 7398 ECE Application Programming: Lecture 22
Functions - pass by address #include <stdio.h>#include <math.h>void get_r_theta(double a, double b, double *adr_r, double *adr_th);void main(){ double x,y,h; printf("Enter x, y components of vector: "); scanf("%lf %lf",&x,&y); // user enters 3,4 get_r_theta(x,y,&r,&th); printf("Vector with x=%lf and y=%lf has r=%lf, theta=%lf\n",x,y,r,th); } void get_r_theta(double a, double b, double *adr_r, double *adr_th) { double sum; sum = pow(a,2)+pow(b,2); //or a*a+b*b; *adr_r = sqrt(sum); *adr_th = atan2(b,a);} x 3.0 4600 y 4.0 4608 r 5.0 4610 th 36.87 4618 ECE Application Programming: Lecture 22
Next time • More pointer examples • PE4—functions ECE Application Programming: Lecture 22