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 25.6 Practice: Structures and Pointers
Check Your Understanding Check Your Understanding
1.
Now, what is wrong with the following piece of code and how would you fix it?
struct student{
char firstName[30];
char lastName[30];
int birthYear;
double aveGrade;
};
int main(void) {
struct student me = {"Petra", "Bonfert-Taylor", 2001, 3.8};
struct student * studentptr = &me;
me->birthYear = 1998;
return 0;
}
The second-to-last line of code should be (&me)->birthYear = 1998;
Correct
The second-to-last line of code should be (*me)->birthYear = 1998;
Not quite. Try again!
The second-to-last line of code should be (me)->birthYear = 1998;
Not quite. Try again!
There is nothing wrong with this code.
Not quite. Try again!
2.
Time for some review of multidimensional arrays (this is called
interleaved learning - a method of learning that helps you retain better what you have learned!)
Suppose you have written a function that initializes a two-dimensional array as follows:
void initialize_array(int array[][NCOLS])
{
int i,j;
for (i=0; i<NROWS; i++)
for (j=0; j<NCOLS; j++)
array[i][j] = i;
}
Assume that you also have
#define NROWS 20
#define NCOLS 30
In your main program you have declared an array matrix as follows:
int matrix[NROWS][NCOLS];
What is the correct function call of the function
initialize_array() from within your main function?
initialize_array(matrix);
Correct
initialize_array(matrix[NROWS][]);
Not quite. Try again!
initialize_array(matrix[][NCOLS]);
Not quite. Try again!
initialize_array(matrix[NROWS][NCOLS]);
Not quite. Try again!
initialize_array(&matrix);
Not quite. Try again!
3.
Please complete the following program so that the function
maxTemp() finds the maximum value stored in the array passed to it. Be sure to enter your prototype at the top of the program and donโt forget to insert the correct function call into the main function. Store the value returned by the function
maxTemp() in the variable "maximum" so that it gets printed out properly in the following line.
admin ..... open in new window
When your program performs correctly youโll be given a keyword to enter in Canvas.
If your code appears to perform correctly but you are not given a keyword, please check for any extra spaces or line breaks in your output and remove them.
4.
Which of the following properly defines a structure:
struct point { int x; int y; }
Correct
struct point { int x; int y; };
Not quite. Try again!
struct point { int x; int y; }
Not quite. Try again!
struct point int x; int y;
Not quite. Try again!
5.
Which of the following properly declares a variable z (i.e. an instance) of structure point?
struct point z;
Correct
struct z point;
Not quite. Try again!
struct point;
Not quite. Try again!
point z;
Not quite. Try again!
6.
Which of the following properly declares a new type โpoint_tโ?
typedef struct {
int x;
int y;
} point_t;
Correct
typedef struct point {
int x;
int y;
} point_t;
Not quite. Try again!
typedef struct point {
int x;
int y;
};
Not quite. Try again!
7.
Suppose you have declared a new type โstudent_tโ as follows:
typedef struct {
char name[30];
long id;
int class;
} student_t;
What is the correct way to declare and initialize a variable me of type
student_t?
student_t me = {โPetraโ, 123456L, 1994};
Correct
me = student_t{โPetraโ, 123456L, 1994};
Not quite. Try again!
student_t = {โPetraโ, 123456L, 1994} me;
Not quite. Try again!
student_t me; me = {โPetraโ, 123456L, 1994};
Not quite. Try again!
8.
As in the previous problem, you have defined a structure to store some student information as follows:
typedef struct {
char name[30];
long id;
int class;
} student_t;
In your main program, you have furthermore declared a variable "me" of type
student_t and initialized it, for example as follows:
student_t me = {"Petra", 123456L, 1994};
Youโd now like to print the student record to the screen (i.e. print what is stored in the variable
me). How would you do this?
Feel free to use the following window to try things out. I have already given you a bit of code to make things easier.
admin ..... open in new window
9.
Letโs continue developing the previous problem. This time weโll practice how to change entries in a structure. Complete the following C program so that the user can enter a new student name, id and class year to be stored in the variable me of type
student_t. All you need to do is complete the
scanf() and
printf() lines.
admin ..... open in new window
When your program performs correctly youโll be given a keyword to enter in Canvas.
If your code appears to perform correctly but you are not given a keyword, please check for any extra spaces or line breaks in your output and remove them.