operator<<
John Cormie's Radio Weblog

 






Subscribe to "operator<<" in Radio UserLand.

Click to see the XML version of this web page.

Click here to send an email to the editor of this weblog.

 

 

  Monday, September 15, 2003


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.
5:21:36 PM     comment []



Click here to visit the Radio UserLand website. © Copyright 2004 John Cormie.
Last update: 3/21/2004; 10:29:46 AM.

September 2003
Sun Mon Tue Wed Thu Fri Sat
  1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30        
Aug   Oct