P.Lecture - 3.6.08

P.Lecture - 3.6.08 - Announcements Assignment 4 Due today...

Info iconThis preview shows pages 1–11. Sign up to view the full content.

View Full Document Right Arrow Icon
Announcements Assignment 4 ue today Due today Exam 2 weeks from today pics for today Topics for today More on pointers Breaking up large programs into separate files Structures, Unions, Enums
Background image of page 1

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

View Full Document Right Arrow Icon
hat’s on e Exam? What s on the Exam? am 2: March 20 Exam 2: March 20 Topics (Everything since Exam 1) ction 9.6 (Recursive Functions) Section 9.6 (Recursive Functions) Chapter 11 (Pointers) Chapter 12 (Pointers and Arrays) Chapter 13 (Strings) Chapter 15 (Writing Large Programs–Today) Chapter 16 (Structs, Unions, Enums – Today) Chapter 17 (Advanced Pointers – 3/18) hapter 22 (I/O) Chapter 22 (I/O)
Background image of page 2
ROBLEMS WITH POINTERS PROBLEMS WITH POINTERS
Background image of page 3

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

View Full Document Right Arrow Icon
Pointers as Parameters r a function to modify an argument variable it must receive a /* example of using pointers as parameters */ id (i t* it * ) For a function to modify an argument variable it must receive a pointer void swap(int*, int*); int main( ) { int a =1, b =2; rintf ( ”the value of a and b are: %d %d ”, a, b); printf ( the value of a and b are: %d %d\n, a, b); swap(&a, &b); printf ( ”the value of a and b are: %d %d\n”, a, b); return 0; } void swap(int *i, int *j) { int t; = *i; t = i; *i = *j; *j = t; return;
Background image of page 4
Returning a Pointer from a Function int *max(int *, int *) int main( ) { int x = 7, y = 9; int *p; p = max (&x, &y); printf (“the larger is %d ”, *p); turn 0; return 0; } int *max(int *a, int *b) { ( , ) { if (*a > *b) return a; else return b; } Never return a pointer to a local variable
Background image of page 5

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

View Full Document Right Arrow Icon
ere is a tricky one Here is a tricky one char *replace(int); ar s[80] = "Hello There"; char s[80] = Hello There ; int main( ) { *replace(5) = 'X'; printf (“%s\n”, s); return 0; } char *replace(int i) { return &s[i]; []; }
Background image of page 6
Problems with Pointers Beware the wild pointer it’s the most difficult bug to find bad pointer can get you garbage or cause you to A bad pointer can get you garbage or cause you to write over other code modules or even the O/S Common errors Unitialized pointers int x =10, *p; *p = x; isunderstood usage of pointers Misunderstood usage of pointers int x =10, *p; p = x; /* oops, p = &x */
Background image of page 7

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

View Full Document Right Arrow Icon
Problems (cont) * the purpose is to output the hex equivalent of a series of strings / the purpose is to output the hex equivalent of a series of strings of chars that have been input by the user (until QUIT) is entered */ #include <string.h> #include <stdio.h> int main( ) { char *p1=0; ar s[80] This program has a logic defect can you find it? char s[80]; p1 = s; hile(!strcmp(s, “QUIT”)) {/* sentinel check */ while(!strcmp(s, QUIT )) {/ sentinel check / /* output the hex equivalent of each character */ while(! (*p1= ‘\0’)) { printf (“%x\n”, *p1); p1++; } gets(s); /* read the next input string */ } return 0; } p1 = s;
Background image of page 8
RITING LARGE PROGRAMS WRITING LARGE PROGRAMS
Background image of page 9

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

View Full Document Right Arrow Icon
Program Production Process library functions
Background image of page 10
Image of page 11
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page1 / 33

P.Lecture - 3.6.08 - Announcements Assignment 4 Due today...

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

View Full Document Right Arrow Icon
Ask a homework question - tutors are online