What is the method for invoking a C language structure?
In C language, the following methods can be used to invoke a structure:
- Accessing structure members directly through the structure variable name:
- The student structure contains an ID, a name, and an age. A student named John with an ID of 1 and age of 20 has been initialized.
- Accessing structure members through pointers.
- There is a structure called Student that contains an id, a name, and an age. An instance of Student named stu is created, and a pointer to the struct called pStu is assigned to point to stu. The id of pStu is set to 1, the name to “John”, and the age to 20.
- Passing a structure through function parameters.
- Define a structure named Student with three data members: id, name, and age. Create a function setStudent that takes a pointer to a Student structure as input and assigns values for id, name, and age. Instantiate a Student structure named stu and call the setStudent function passing a reference to stu.
All of the above methods can be used to call a struct, as well as assign values to or access members of the struct.