Worksheet: C1 | CS 2113 Software Engineering - Fall 2022

Worksheet: C1

Worksheets are self-guided activities that reinforce lectures. They are not graded for accuracy, only for completion. Worksheets are due by Sunday night before the next lecture.

Questions

  1. For each of the statements, indicate if it evaluates to True? (Assume all needed headers are included).

    a) sizeof(int) == sizeof(int *)
    b) sizeof(char) == sizeof(char *)
    c) sizeof(short) == sizeof(short *)
    d) sizeof(long) == sizeof(long *)
    e) sizeof(char *) == sizeof(short *)
    

    Reveal Solution

  2. Examine the following program closely, and describe what happened when it is run. What output do you expect?

    unsigned int bigger_largest_uint = 4294967295 + 1; //largest_uint + 1
    int bigger_largest_int = 2147483647 + 1; //largest_int + 1
    
    //Before you run this, what do you expect the value to be
    if (4294967295 < bigger_largest_uint){
        printf("A");
    }
    else{
        printf("B");
    }
    
    if (2147483647 < bigger_largest_int){
        printf("C");
    }
    else{
        printf("D");
    }
    

    Reveal Solution

  3. Describe a situation in which the difference between x++ and ++x would make a significant difference in the operation of the program.

    Reveal Solution

  4. Why does the expression

    (3 & 4) && (1)
    

    differ from

    (3 && 4) && (1)
    

    Your answer should be specific to the output and the underlying reason why they differ.

    Reveal Solution

  5. What is the output for a, b, and c?

       int a = 1 << 1;
       int b = a << 1;
       int c = b << 2;
    

    Can you find a closed-form formula for shifting bits, using the variables x, y, and z?

       int z = x << y
    

    Reveal Solution

  6. As we discussed in class a pointer (*) is a variable that stores a memory address. Pointers are further distinguished by the data type that lies at the store memory address. For example int * p; declares a variable p that is a pointer to an integer data type. Another way to think about this is p stores the address of a place in memory, that contains integer data.

    Also remember that we can assign a pointer with the & operator, which gets the address of the variable it is applied to.

    int c = 45;
    int * p = &c;
    

    Based on your best reasoning, answer the questions below about pointers (which we will officially cover in future classes). Try your best reason about the questions below without looking them up. Remember, worksheets are about learning and getting experience with class material :)

    1. What do you think the following declaration signifies? (int *) * c;
    2. Do think that (int *) * c; is an equivalent declaration to int ** c;?

    Finally, draw a memory diagram for each of the designated marks. You can upload the drawing or ASCII art to your repo.

    int main(){
        int a = 1;
        int b = 2;
        int c = 3;
        int * p = &a;
        int ** q = &p; //Mark 0
        *p = 15; //Mark 1
        p = &b;
        **q = 0; //Mark 2
    
    }
    

    Mark 0

    .----------.-------.
    | variable | value |
    |----------+-------|
    | a        | 1     | <-.
    |----------+-------|   |        
    | b        | 2     |   |       
    |----------+-------|   |       
    | c        | 3     |   |       
    |----------+-------|   |         
    | p        | .-----|---' <----.        
    |----------+-------|          |   
    | q        |   .---+----------'  
    |------------------|             
    
    

    Reveal Solution

  7. Consider the following snippet of c code

    int a[] = {1, 2, 3, 4};
    short b[] = {1, 2, 3, 4};
    char c[] = {1, 2, 3, 4};
    int * p = a;   
    

    a. What is the sizeof(a[0])?
    b. What is the sizeof(a)?
    c. What is the sizeof(b)?
    d. What is the sizeof(c)?
    e. What is the sizeof(p)?
    f. What is the sizeof(*p)?
    g. Explain the differences in the sizeof for each of these output?

    Reveal Solution

  8. Consider the following snippet of c code. Upload an image to the repo and/or include it in the Markdown file. (You can also do it in ASCII art, like in lecture notes).

    int a = 5;
    int b = a;
    int c[2];
    
    int *p1 = &a;
    int *p2 = c;
    *p2 = b;
    c[1] = a;
    //MARK 1
       
    *p1 = *p2;
    b = 20;
    //MARK 2
       
    

    a. Draw the memory diagram at MARK 1
    b. Draw the memory diagram at MARK 2

    Reveal Solution

  9. For the following snippet of c code, draw a memory diagram for each step in the for loop at the MARK. (This requires drawing 4 memory diagrams) Upload an image to the repo and/or include it in the Markdown file. (You can also do it in ASCII art, like in lecture notes).

    #include <stdio.h>
    
    int foo[4] {4, 7, 3, 9};
    int * bar = NULL;
    int * other = NULL;
        
    int main() {
        for (int i = 0; i < 4; i++) {
            if (i % 2 == 0) {
                foo[i] = foo[i] % 2;
                bar = &(foo[i])
            }
            else {
                foo[i] = foo[i] * 2;
                other = &(foo[i])
            }
        //MARK
        }
    }
    

    Reveal Solution

  10. Fill in the sum function so that it adds the elements in array.

    #include <stdio.h>
    
    int sum(<ARGUMENTS GO HERE!>) {
        //Code goes here!
    }
    
    int main() {
        int array[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
        int s = sum(array);
        printf("%d\n", s);
    }
    

    Reveal Solution

  11. Using the following struct declaration, print out the f member of FOO. (Hint: It might be helpful to first figure out a value that you can then debug with!)

    #include <stdio.h>
    
    int main() {
    
        struct bar {
            float f;
            double d;
        };
    
        struct foo {
            int i;
            int j;
            struct bar * BAR;
        };
        
        struct foo * FOO;
    
        // ... some code that sets up foo!
        
        printf("f = %g", <FILL-IN-HERE> ); //finish this line.
    }
    

    Reveal Solution

  12. Using the following struct definition:

    typedef struct {
        short head;
        int left_arm;
        int * right_arm;
        char left_leg;
        long * right_leg;
    } frankenstein;
    
    frankenstein monster;
    
    
    //size of the monster's parts
    int head_length = sizeof(monster.head);
    int left_arm_length = sizeof(monster.left_arm);
    int right_arm_length = sizeof(monster.right_arm);
    int left_leg_length = sizeof(monster.left_leg);
    int right_leg_length = sizeof(monster.right_leg);
    int height = sizeof(monster);
    

    First, without running the code, write down the expected sizes of each of the int length variables above, including height.

    Second, write a program to reveal the actual sizes. The height may differ than what you expect, right? Why might you think that’s case. (We’ll go over the true reason in class)

    Reveal Solution