Skip to main content
Logo image

Dive Into Systems: Exercises

Section 2.9 Advanced C Features

Subsection 2.9.1 Constants, switch, enum, and typedef

Checkpoint 2.9.1. Fill In The Blank.

What is printed in the following code fragment?
void test1(int a) {

    switch(a) {
        case 1:
            printf("%d ", a);
            break;
        case 2:
            printf("%d ", a);
            break;
        case 3:
            printf("%d ", a);
            break;
    }

}

int main(void) {

  test1(1);

  return 0;
}

Checkpoint 2.9.2. Fill In The Blank.

What is printed in the following code fragment?
void test1(int a) {
    switch(a) {
        case 1:
            printf("%d ", a);
        case 2:
            printf("%d ", a);
        case 3:
            printf("%d ", a);
    }

}

int main(void) {

  test1(1);

  return 0;
}
Hint.
Is there a missing break statement?

Checkpoint 2.9.3.

Rewrite the test1 function using switch instead of chaining if-else-if
Hint.
Did you remember to use break and default?
Answer.
Here is an example solution:
int test1(int a) {
    int b;

    switch(a) {

        case 1:
            b = 20;
            break;
            
        case 2:
            b = 25;
            break;

        case 3:
            b = -5;
            break;

        default:
            b = 2;
    }
    return b;
}

Subsection 2.9.2 Command Line Arguments

Checkpoint 2.9.4. Fill In The Blank.

Consider the following program:
int main(int argc, char *argv[]) {

    int i;

    for (i = 0; i < argc;  i++) {
        printf("%s", argv[i]);
    }

    return 0;
}
What will the program print if it is compiled to an executable named cli and run on the command line with the following arguments?
What is the value of argc?
Now suppose the program was run with the following arguments:
What will the program print out? What is the value of argc?
Hint 1.
What is argv[0]?
Hint 2.
An argument passed within quotation marks is considered to be a string literal, which behaves as a single argument, even if it contains spaces.

Subsection 2.9.3 The void* Type

Checkpoint 2.9.5. void* with malloc.

    The following is the function prototype for malloc:
    void *malloc(size_t size);
    What is the correct variable declaration for arr in the following call to malloc:
    arr = malloc (sizeof(float) * 100);
  • void* arr;
  • Incorrect.
  • float* arr;
  • Correct!
  • void arr;
  • Incorrect.
  • float arr;
  • Incorrect.
  • None of these choices are valid.
  • Incorrect.

Checkpoint 2.9.6. void* for dynamic typing.

    Here is a puzzle; consider a function named any_print that takes a pointer to any type and prints its value (if it exists):
    void any_print(void *p, int choice) {
    
        int x;
        float y;
        long z;
    
        if(p == NULL) {
           printf("Error\n");
           return;
        }
    
        if (choice == 1) {
            x = *((int *)p);
            printf("%d\n", x);
        }
        else if (choice == 2) {
            y = *((float *)p);
            printf("%f\n", y);
        }
        else {
            z = *((long *)p);
            printf("%ld\n", z);
        }
    }
    
    Which of the following are correct ways to call this function?
  • int alice;
    alice = 6;
    any_print((void *) &alice, 1);
    
  • Correct!
  • int bob;
    bob = 42;
    any_print((void *) &bob, 2);
    
  • Incorrect.
  • long carlos;
    eve = -100;
    any_print((void *) &carlos, 0);
    
  • Correct!
  • float dora;
    dora = -6.24;
    any_print((void *) &dora, 2);
    
  • Correct!
Hint.
Look at what type the passed in parameter is getting converted to based on the choice parameter.

Subsection 2.9.4 Pointer Arithmetic

Checkpoint 2.9.7. Pointer Math.

Given the following variable declarations:
int *ip;
float *fp;
double *dp;
char *cp;
How many bytes forward in memory would each of the following operations move their respective pointer?
ip++ bytes
fp++ bytes
dp++ bytes
cp++ bytes
Hint.
Assume integers are 4 bytes, floats are 4 bytes, doubles are 8 bytes, and chars are 1 byte.

Checkpoint 2.9.8. Pointer Math (arrays).

This code uses pointer math to set all elements of an array to 100. Put the lines of code in order.
Hint.
Hint: Do any of the statements overwrite each other?

Checkpoint 2.9.9. Pointer arithmetic for arrays.

    Here are some code snippets that attempt to set every other element in an int array named arr to 240 using pointer arithmetic:
    #define SIZE 100
    int arr[SIZE];
    int *it; 
    int i;
    
    Which are correct?
  • for (it=arr; it < arr+SIZE; it+=2) { 
       *it = 240;
     }
    
  • Close!
  • it = arr;
    for (i = 0; i < SIZE/2; i++) {
      *it = 240;
       it = it +2;
    }
    
  • Close!
  • for (it=arr; it < SIZE; it+=2) { 
       *it = 240;
    }
    
  • Incorrect
  • Both A and B.
  • Correct!
  • Both A and C.
  • Incorrect.
  • Both B and C.
  • Incorrect.
  • all of these.
  • Incorrect.
Hint.
Make sure that elements 0,2,4,6,8... are being set to 240, and not elements 1,3,5,7,9...