Answer all questions. Do so on a separate sheet of paper.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
main(int argc, char **argv)
{
char *s, *u, *v;
s = argv[1];
v = strchr(s, 'G');
u = strdup(s);
if (v == NULL) exit(0);
*v = 'g';
v += 5;
*v = 'i';
printf("0x%x 0x%x\n", s, u);
printf("argc: %d\n", argc);
printf("argv[1]: 0x%x %s\n", argv[1], argv[1]);
printf("s: 0x%x %s\n", s, s);
printf("u: 0x%x %s\n", u, u);
printf("v: 0x%x %s\n", v, v);
printf("strstr: 0x%x %s\n", strstr(u, "ge"), strstr(u, "ge"));
}
|
Part 1: Suppose we compile this into an executable called pex, and run it from the shell as below. I give the first line of output. What are the remaining lines of output? Put question marks where you don't know the answer, but answer everything that you can.
UNIX> pex Curious-George Prince-George Eddie-George 0xbfffedd8 0x3000f0 |
Part 2: You'll note that the above program does no error checking. Give two examples of running it from the command line where the output will be non-deterministic (e.g. a Bus error, Segmentation violation, or some kind of random output) and state why these examples are problemmatic. Note, you don't need to specify what the output is, just why the examples are problemmatic.
![]()
|
Implement the three procedures: new_list(), list_prepend(). and free_list().
#include <stdio.h>
#include <stdlib.h>
#include "fields.h"
#include "stack.h"
#include "queue.h"
#include "dllist.h"
main()
{
IS is;
Queue q;
Stack s;
Dllist l;
int i;
Jval j;
is = new_inputstruct(NULL);
while (get_line(is) > 0) {
q = new_queue();
s = new_stack();
l = new_dllist();
for (i = 0; i < strlen(is->fields[0]); i++) {
queue_enqueue(q, new_jval_c(is->fields[0][i]));
stack_push(s, new_jval_c(is->fields[0][i]));
dll_append(l, new_jval_c(is->fields[0][i]));
dll_prepend(l, new_jval_c(is->fields[0][i]));
}
while (!queue_empty(q)) {
j = queue_dequeue(q); printf("%c", j.c);
j = stack_pop(s); printf("%c", j.c);
printf("%c", l->flink->val.c); dll_delete_node(l->flink);
printf("%c", l->flink->val.c); dll_delete_node(l->flink);
printf(" ");
}
printf("\n");
free_queue(q);
free_stack(s);
free_dllist(l);
}
}
|
What will the output of this program be when the following file is given as standard input:
1 2 3 Fred Luther Dontonio Eddie Prince Curious Frank Lloyd Wright 22 |
|
|
|