home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C++ for Dummies (3rd Edition)
/
C_FD.iso
/
WORKOUT
/
PART1.TXT
next >
Wrap
Text File
|
1996-09-06
|
3KB
|
91 lines
25-Minute Workout
Part I Answers
Hey! Go back and try to answer the questions on your own first.
1
a. int array[10]
b. int *pInt
c. int *arrayPtrs[10]
Note that [] has a higher precedence than *, which is what keeps this from being a
pointer to an array of ten integers.
d. int *fn(void)
e. void (*pFn)(int*)
The parentheses around pFn are necessary to keep this from being a function that
returns a pointer to an integer (like 2d).
2
I gave you a big hint on this one (actually two hints). Without a prototype, C passes both arguments as
integers ù what does it know? The first argument is fine, but fn expects a float for the second argument.
The integer provided is interpreted as garbage.
The solution is to add a prototype:
int fn(int, float);
void anotherFn()
{
int x;
x = fn(1, 2);
}
Some of you might have said, ôAh well, letÆs just change the call.ö
void anotherFn()
{
int x;
x = fn(1, 2.0);
}
This doesnÆt work either. To maintain compatibility with the older K&R C, a float is passed as a double in
the absence of a prototype. Thus, the second argument ends up at fn() as a double and not a float. As
Homer Simpson would say, ôDOOO.ö
3
The answer is ômaybe nothing,ö depending on your compiler. The potential problem is in the printf()
statement:
printf(ôarray[%2d] = %d, array[%2d] = %d\nö,
i, pArray[i++], i, pArray[i++]);
Notice that this expression contains many subexpressions that have mutual side effects, namely i++. The
programmer of this statement is assuming that the subexpressions are evaluated from left to right. They
might well be, but ANSI C doesnÆt guarantee it. (Under Borland and Turbo C++, for example,
subexpressions passed to a function are evaluated from right to left.)
Adding parentheses doesnÆt help. The only way to solve this problem is to break up the expression into
several independent expressions, as follows:
printf(ôarray[%2d] = %d, ô, i, pArray[i]);
i++;
printf(ôarray[%2d] = %d\nö , i, pArray[i]);
i++;
4
The following function does the trick:
#include <stdio.h>
void fn(int *pX, int *pY)
{
int localX, localY;
/*first read value into local variables*/
printf(ôEnter x & y:ö);
scanf(ô%d%dö, &localX, &localY);
/*now copy into callerÆs arguments*/
*pX = localX;
*pY = localY;
}
int main()
{
int x, y;
fn(&x, &y); /*have to pass the address here*/
return 0;
}
This function first reads the data values into local variables, and then copies the variables to the callerÆs x
and y variables.
Notice that the local variables are not necessary. The following works just fine:
#include <stdio.h>
void fn(int *pX, int *pY)
{
printf(ôEnter x & y:ö);
scanf(ô%d%dö, pX, pY);
}
5
The function sizeOfMyStruct() will always return a 1, irrespective of the actual size of a MyStruct structure.
Remember that pointer addition and subtraction are always defined in terms of the sizes of the objects
pointed to. Therefore, &x[i]û&x[j] is equal to iûj no matter what the type of x. Use the sizeof keyword to
return the size of a structure or an object.