CS 241 Fall 2007
System Programming

 

 

10-10:50am MWF

1404 Siebel Center for Computer Science

 

Home

Schedule and Lectures

Compass

News

 

From: Tarek Abdelzaher

Date: 8/24/2007

 

Hi all,

 

Here is a set of questions that test your ability to handle certain special difficulties and subtleties in C, courtesy of Steve Kloder, your TA. Please take a look. If you are not sure of most of your answers, you probably need to come to the remedial C crash course Sunday 8/26 at 4pm, in 4405 Siebel Center. Have a great weekend!

 

Tarek

 

 

 

Each of these examples can appear inside C programs.  The ... means additional (irrelevant) instructions.  For each example,
  a) What does the code attempt to do?
  b) Why does it not work correctly?
  c) How can the code be corrected?

1)
    char s[50];
    scanf("%50s",s);
    s[50] = '\0';

2)
    int* x,y;
    ...
    printf("%d\n",*y);

3)
    char somespace[8];
    strcat(somepace,"12345678");

4)
    char *str;
    int x;
    ...
    switch(str) {
       case "one":    x=1; break;
       case "two":    x=2; break;
       case "three:    x=3; break;
       default:    x=0;
    }

5)
    int i;
    ...
    while(i>0);
       printf("i=%d\n",i--);

6)
    void test(char*s1, char*s2) {
            return strcmp(s1,s2) == 0;
    }

7)
    char *str = "abc";
    strcat(str,"def");

8)
    int *array;
    int i,n;
    ...
    for(i=0;i<n;i++)
        array[i] = i*i;

9)
    int x;
    int *p = x;

10)
    char s1[5] = "word";
    char s2[5] = "word";
    if (s1 == s2)
        printf("The strings are equal.\n");

11)
    int x, *p;
    p = *x;

12)
    int **array;
    int i,j,10;
    array = malloc(10*sizeof(int *));
    for(i=0;i<10;i++)
        for(j=0;j<10;j++)
        array[i][j] = i*j;

13)
    int main(int argc, char *argv[]) {
            printf(argv[1]);
    }

14)
    void increment(int x) {
        x++;
    }
    ...
    for(x=0;x<10;increment(x)) {
        printf("x=%d\n",x);
    }

15)
    char str[5];
    strncpy("abcde",str,5);

16)
    int i;
    ...
    switch(i) {
       case 1:    printf("One.\n");
       case 2:    printf("Two.\n");
       case 3:    printf("Three.\n");
       default:    printf("Something Else.\n");
    }

17)
    char c = "a";

18)
    int i,x[10];
    for(i=0;i<=10;i++)
      x[i]=i*2;

19)
    int x;
    ...
    int *p = (int *)x;

20)
    char *str,*p;
    ...
    p=str;
    while(*p!=0) {
        *p++;
        p++;
    }

21)
    int x=5;
    if (x=1)
        printf("one.\n");
    else
        print("not one.\n");

22)
    char str[3] = "abc";

23)
    int x,y;
    char s[50];
    scanf("%d %d %5s",x, y, s);

24)
    void reset(int *x) {
        x=&0;
    }
    ...
    x=1;
    reset(&x);
    printf("x is now 0.\n");

25)
    int *array;
    int i,j;
    for(i=0;i<5;i++) {
        array = malloc(5*sizeof(int));
        for(j=0;j<5;j++)
        array[j] = i*j;
    }

26)
    int x, *ip;
    float y, *fp;

    ...
    ip = &x;
    fp = (float *)ip;
    y = *fp;

27)
    int x,*p;
    *p=x;

28)
    int x;
    void *p;
    p = &x;
    ...
    printf("%d\n",*p);

29)
    int **array = (int **) malloc(n*sizeof(int));

30)
    int x;
    scanf("%f",&x);

 


Updated: August 24, 2007