Programming Quiz #3
This one is pretty tricky. The programmer expected the code below to parse out the words of each semi-colon seperated line. However, it doesn't seem to process anything past the first semi-colon. What's wrong?
void process(char *str)
{
char *ptr;
ptr = strtok(str, " ");
printf("Some words: ");
while (ptr) {
printf("%s ", ptr);
ptr = strtok(NULL, " ");
}
printf("\n");
}
int main(void)
{
char *ptr;
char buf[80];
fgets(buf, 80, stdin);
ptr = strtok(buf, ";");
while (ptr) {
process(ptr);
ptr = strtok(NULL, ";");
}
}
Answer to quiz #2: Think about the order in which constructors are run. Passing a pointer to a member to the base class constructor is risky because the constructor for the member hasn't been run yet.
No comments:
Post a Comment