CS360: Exam 1: 10/22/97. Question 2 (12 points)
For each of the following pieces of code, explain what the output will
be. Assume that the following is typed into standard input:
J S P
HBP
K M P
If the code exits abnormally, state where in the code this happens and
why.
A
#include < stdio.h >
#include "fields.h"
main()
{
IS is;
int i, j;
is = new_inputstruct(NULL);
while(get_line(is) >= 0) {
j = is->NF-1;
while (j >= 0) {
printf("%s ", is->fields[j]);
j--;
}
printf("\n");
}
}
B
#include < stdio.h >
#include "dlist.h"
main()
{
char s[100];
Dlist d, tmp;
d = make_dl();
while(gets(s) != NULL) {
dl_insert_a(d, s);
}
dl_traverse(tmp, d) printf("%s\n", tmp->val);
}
C
main()
{
char buffer[1000];
char *ptr;
char *s1 = "JIM";
char *s2 = "PLANK";
int *iptr;
ptr = buffer;
iptr = (int *) ptr;
*iptr = strlen(s1);
ptr += sizeof(int);
strcpy(ptr, s1);
ptr += strlen(s1);
iptr = (int *) ptr;
*iptr = strlen(s2);
ptr += sizeof(int);
strcpy(ptr, s2);
ptr += strlen(s2);
write(1, buffer, ptr-buffer);
}