Skip to main content\(\newcommand{\R}{\mathbb R}
\newcommand{\lt}{<}
\newcommand{\gt}{>}
\newcommand{\amp}{&}
\definecolor{fillinmathshade}{gray}{0.9}
\newcommand{\fillinmath}[1]{\mathchoice{\colorbox{fillinmathshade}{$\displaystyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\textstyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\scriptstyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\scriptscriptstyle\phantom{\,#1\,}$}}}
\)
Section 2.9 Advanced C Features
Subsection 2.9.1 Constants, switch, enum, and typedef
Checkpoint 2.9.1. Fill In The Blank.
Checkpoint 2.9.2. Fill In The Blank.
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.
Hint 1.
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.
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.
#define SIZE 100
int arr[SIZE];
int *ptr;
---
ptr = arr;
---
for (int i = 0; i < SIZE; i++) {
---
*ptr= 100;
---
ptr++;
---
}
---
for (int i = 0; i < SIZE; i += 4) { #distractor
---
ptr = 100; #distractor
---
*ptr++; #distractor
---
ptr += 4; #distractor
---
(ptr +1) = 100; #distractor
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...