Accessing the Grade Field in a Structure

How to access the grade field in a structure?

Given the following code, what is/are correct way to access the grade field? struct courseInfo { Int totalMark; char grade; char courseName[100]; }; struct courseInfo CS1; struct courseInfo *CS2; (a) CS1.grade (b) CS2.grade (c) CS1->grade (d) Both (a) and: CS2->grade

Answer:

The correct way to access the grade field is (a) CS1.grade and (c) CS1->grade.

Is it possible to access the grade field using CS1.grade or CS1->grade?

To access the grade field in the given code, we have two options. In (a) CS1.grade, we directly access the grade field of the CS1 structure using the dot operator. This works because CS1 is an instance of the "courseInfo" structure. In (c) CS1->grade, we use the arrow operator to access the grade field when CS1 is a pointer to the "courseInfo" structure. This is valid because the arrow operator is used to access structure members through pointers.

← Thomas s troubleshooting adventure Linear regression analysis for predicting used sedan prices →