which points to a particular student’s record. In order to access a member (for example birthYear) of this student’s record via the pointer we first need to dereference the pointer (*studentptr) and then access the member via the direct member selection operator .:
(*studentptr).birthYear
The parentheses around (*studentptr) are important since without them the computer would attempt to execute the member selection operator first, which would make no sense, given that studentptr is not a structure but rather a pointer to a structure.
The indirect member selection operator -> combines these steps into one:
studentptr->birthYear
and therefore accomplishes the same thing as the above. Watch the video to find out more:
If you cannot see this codecast, please click here.
Check Your UnderstandingCheck Your Understanding
1.
Now, what is wrong with the following piece of code and how would you fix it?
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
as part of your code.
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.
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.
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.
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.