Midterm 1

Table of Contents

Homeworks and in-class assignments are where midterm questions are tested.

1. Topics

  • Make Variables
  • The build/compilation sequence
  • Size of all data types
  • All keywords except union and volatile
  • Precedence vs. Associativity
  • The sequence points (; and ,)
  • Return values of the I/O functions
    • prinf returns the amount of characters that it wrote
    • scanf returns the number of successful conversions it made
    • getchar returns an int to circumvent the inband-signaling of an incorrect character
    • putchar returns the value of the character written
  • Passing pointers and pointers to pointers
  • Endians
  • Structs
  • When are read only strings read only?
  • Storage Class
  • gdb commands

2. Expected Questions

  • Changing the value of a pointer in a function
  • Precedence between * (dereference) and other operators (specfically . and ++)

3. Example Coding Problem

25 points: Coding question #1 – Write the call to a function swapIfWrong and write the function. There are two int pointers and a function pointer available in the calling function to be passed to swapIfWrong. How the pointers and function pointer got their values is not shown and is not your concern; assume valid values are present in all 3 and that those values were assigned where comment #3 is. After swapIfWrong runs, the goodptr will point to the better value and the badptr will point to the worse value. Do not change the values; only change the pointers if they need it. To compare the values, use the isbetter comparison function. The isbetter function returns a true value if the goodvalue it is passed is better than the badvalue it is passed.

Be very careful about type. Think about what it means when a function needs to change something. The structure needed here is somewhat simple, the types of the variables require great care.

Again: When swapIfWrong returns, goodptr and badptr in the calling function now point where they should.

Your coding task:

  1. Fill out the arguments to swapIfWrong in the calling function (complete the line between comment #3 and #4).
  2. Fill out the parameter list for swapIfWrong (just below comment #5).
  3. Write the code for swapIfWrong (between comment #6 and #7).
/* #1 assume are required #includes and declarations have been made */
typedef int (*comparison_function)(int goodvalue, int badvalue);
int CallingFunction() {
    int *goodptr, *badptr;
    comparison_function is_better;
    /* #2 code you did not have to write has given a value to
    ** goodptr, badptr, and is_better. That code would be here.*/
    /* #3 fill in the function parameters below */
    swapIfWrong(/* enter parameters */);
    /* #4 the line above is all you do in the calling function */
}
/* put your function below here */
void swapIfWrong() {
}

3.1. My Solution

#include <stdio.h>
/* #1 assume are required #includes and declarations have been made */
typedef int (*comparison_function)(int goodvalue, int badvalue);
/* put your function below here */
void swapIfWrong(int **good, int **bad, comparison_function comp) {
    int *temp;

    if (!comp(**good, **bad)) {
        /* I aussume this means that we have a bad value */
        temp = *good;
        *good = *bad;
        *bad = temp;
    }

}


int CallingFunction() {
    int *goodptr, *badptr;
    comparison_function is_better;
    /* #2 code you did not have to write has given a value to
    ** goodptr, badptr, and is_better. That code would be here.*/
    /* #3 fill in the function parameters below */
    swapIfWrong(&goodptr, &badptr, is_better);
    /* #4 the line above is all you do in the calling function */
}
/* I need a comparison function */
int greater(int big, int small) {
    printf("Comparing %d to %d\n", big, small);
    return (big > small);
}

void main() {
    int *goodptr, *badptr;
    comparison_function is_better;
    int bigi = 4, smalli=20;
    goodptr = &bigi;
    badptr = &smalli;
    is_better = greater;
    printf("Before *good is %d and *bad is %d\n", *goodptr, *badptr);
    /* pass in the address of things I need to change */
    swapIfWrong(&goodptr, &badptr, is_better);
    printf("After *good is %d and *bad is %d\n", *goodptr, *badptr);
    printf("Hello!");
}

Author: Jackson Daumeyer

Created: 2022-10-06 Thu 12:30