Quiz C2-2
Submit your answers via this Google Form
Question 1
What does the program below print out?
#include <stdio.h>
void super_cool_function(char *p){
*p = *p - 4;
*p++;
}
int main() {
char a[6] = {'L','I','P','P','S', 4};
for(int i =0; i < 6; i++){
char * b = &a[i];
super_cool_function(b);
}
printf("%s",a);
}
Question 2
What does the program below print out?
#include <stdio.h>
#include <string.h>
int main() {
char str[] = "This will print";
char * ptr;
for( ptr = &str[strlen(str)-1]; ptr-str > 2; ptr--);
printf("%s",ptr);
}
Question 3
This program isn’t nice and tells us to go away. Select the matching line(s) that will fix the program and make it say “Welcome”.
#include <stdio.h>
#include <string.h>
typedef struct {
char **value;
char *another_value;
} thing_t;
int main() {
char *string1 = "a b c ";
char **string_ptr = &string1;
thing_t thing;
thing.another_value = string1++;
thing.value = &(thing.another_value);
if (strcmp(string1, *(thing.value)) == 0) {
printf("Welcome!");
} else {
printf("Go Away!!");
}
}
Question 4
Consider the program below
int main(int argc, char * argv[]){
for(char ** str = argv; *str; str++){
printf("%c ", *(*str+1));
}
printf("\n");
}
Say that we compile this program to a.out
and run it with the following arguments. What is the output?
./a.out foo bar baz boo bee bop