{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}



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

View Full Document Right Arrow Icon
CS 31: INTRODUCTION TO COMPUTER SCIENCE I Computer Science Department University of California, Los Angeles Dr. John A. Rohr March 3, 2009 HOMEWORK 4 SOLUTION Practice with Pointers 1. The program has two problems. First, because of the order of precedence of the operators, the expression *ptr + 1 = '*' means (*ptr) + 1 = '*' . The expression (*ptr) + 1 evaluates to the character '*' which cannot appear on the left side of an assignment statement. When corrected to *(ptr+1) = '*' , the expression then means *(&arr[1]) = '*' , which means arr[1] = '*' . Second, the while loop prints elements in the wrong order, doesn't access arr[2], and tries to access arr[-1]. One way to fix this is the following: for (ptr = arr; ptr < arr + 3; ptr++) cout << *ptr; 2. The findUnordered function puts the correct value in pBad, but pBad is a copy of the caller's variable ptr, so findUnordered has no effect on the variable ptr. The parameter pBad must be passed by reference, not by value: void findUnordered(double arr[], int n, double* & p) 3. The declaration double* pa; declares pa to be a pointer to double, but leaves it uninitialized -- it does not point to any particular double. That uninitialized
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
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}