Skip to main content
Contents Index
Search Book
Search Results:
No results.
Readability settings Prev Up Next
\(\newcommand{\N}{\mathbb N}
\newcommand{\Z}{\mathbb Z}
\newcommand{\Q}{\mathbb Q}
\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 19.12 Practice: Arrays and Pointers
Check Your Understanding Check Your Understanding
1.
Letβs start with a review question!
What is the output of the following program:
#include <stdio.h>
void swap (char *x, char *y);
int main(void) {
char c = 'P';
char d = 'T';
printf("%c%c ", c, d);
swap(&c, &d);
printf("%c%c", c, d);
return 0;
}
void swap (char *x, char *y) {
char temp;
temp = *x;
*x = *y;
*y = temp;
}
PT TP
Correct
PT xy
Not quite. Try again!
PT PT
Not quite. Try again!
PT yx
Not quite. Try again!
None of the above
Not quite. Try again!
2.
Suppose you have declared an array of integers by typing
int numbers[] = {5, -1, 7, 10, 0, -11};
Feel free to use the window below to try out some code. Be sure to work the correct answer out "by hand" first before verifying your answer using the code window.
admin ..... open in new window
0
Correct
-1
Not quite. Try again!
7
Not quite. Try again!
-11
Not quite. Try again!
10
Not quite. Try again!
5
Not quite. Try again!
None of the above
Not quite. Try again!
3.
Suppose you have declared an array of integers by typing
int numbers[] = {5, -1, 7, 10, 0, -11};
What is
*numbers+4? Notice that the only difference to the previous question is the omission of parenthesis.
You really need to think about this first before just trying it out. Really! Anyway, here is the code window for you to try out some code.
admin ..... open in new window
9
Correct
5
Not quite. Try again!
7
Not quite. Try again!
4
Not quite. Try again!
0
Not quite. Try again!
-1
Not quite. Try again!
4.
Suppose (again...) you have declared an array of integers by typing
int numbers[] = {5, -1, 7, 10, 0, -11};
Random and depends on what is at that memory location
Correct
5
Not quite. Try again!
This will cause a crash of your program
Not quite. Try again!
The compiler sets this to zero
Not quite. Try again!
-11
Not quite. Try again!
5.
Suppose (for the last time) you have declared an array of integers by typing
int numbers[] = {5, -1, 7, 10, 0, -11};
You have furthermore declared a pointer via
int *ptr;
ptr = numbers + 2;
You want to try this out, right? Remember... Think first!
admin ..... open in new window
10
Correct
-11
Not quite. Try again!
5
Not quite. Try again!
0
Not quite. Try again!
-1
Not quite. Try again!
7
Not quite. Try again!