Pointers to Structs
The C programming language allows you to create pointers not only to regular data types but also to structures (since a structure is a custom data type).
Declaration and initialization of a pointer looks like this:
struct <name_of_struct>
{
<field_of_struct>;
};
struct <name_of_struct>* <name_of_pointer>;
If <name_of_pointer> is a pointer to a structure,
then *(<name_of_pointer>) is the structure itself.
To access the fields of a structure through a pointer, you must use the -> operator.
-> is an analogue of the structure access operator .
main.c
12345678910111213141516171819202122#include <stdio.h> // structure declaration struct Point { int x; int y; }; int main() { // create a structure variable of Point type struct Point p1; p1.x = 10; p1.y = 20; // create a pointer to a structure of type Point and assign it the address of variable p1 struct Point* ptr = &p1; // Access structure members via pointer by `->` operator printf("Coordinates: (%d, %d)\n", ptr->x, ptr->y); return 0; }
This program shows how to work with a Point structure that stores two coordinates, x and y. In the main function, a p1 variable is created and assigned coordinate values, then a pointer to this structure is initialized. Using the -> operator, the program accesses the structure's fields through the pointer and prints the point's coordinates to the screen.
Swipe to start coding
You have a student with grades in 3 subjects: Math, Physics, and Programming.
Your task is to calculate the student's average grade and determine if they passed the course.
Inside the calculateAverageAndCheck function:
- Access each subject grade using the pointer.
- Add all three grades together to find the total score.
- Divide the total by
3.0to calculate the average grade. - Compare the result to the passing threshold of 60.
- If the average is greater than or equal to 60, return
true(the student passed). - Otherwise, return
false(the student failed).
Example
| Math | Physics | Programming | Average | Result |
|---|---|---|---|---|
| 85.0 | 55.0 | 60.0 | 66.67 | β Passed |
| 50.0 | 40.0 | 45.0 | 45.00 | β Failed |
| 60.0 | 60.0 | 60.0 | 60.00 | β Passed |
Solution
Thanks for your feedback!
single
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat
Can you show me the code example for this Point structure?
How does the `->` operator differ from the dot operator in C?
Can you explain how to initialize a pointer to a structure in more detail?
Awesome!
Completion rate improved to 4.35
Pointers to Structs
Swipe to show menu
The C programming language allows you to create pointers not only to regular data types but also to structures (since a structure is a custom data type).
Declaration and initialization of a pointer looks like this:
struct <name_of_struct>
{
<field_of_struct>;
};
struct <name_of_struct>* <name_of_pointer>;
If <name_of_pointer> is a pointer to a structure,
then *(<name_of_pointer>) is the structure itself.
To access the fields of a structure through a pointer, you must use the -> operator.
-> is an analogue of the structure access operator .
main.c
12345678910111213141516171819202122#include <stdio.h> // structure declaration struct Point { int x; int y; }; int main() { // create a structure variable of Point type struct Point p1; p1.x = 10; p1.y = 20; // create a pointer to a structure of type Point and assign it the address of variable p1 struct Point* ptr = &p1; // Access structure members via pointer by `->` operator printf("Coordinates: (%d, %d)\n", ptr->x, ptr->y); return 0; }
This program shows how to work with a Point structure that stores two coordinates, x and y. In the main function, a p1 variable is created and assigned coordinate values, then a pointer to this structure is initialized. Using the -> operator, the program accesses the structure's fields through the pointer and prints the point's coordinates to the screen.
Swipe to start coding
You have a student with grades in 3 subjects: Math, Physics, and Programming.
Your task is to calculate the student's average grade and determine if they passed the course.
Inside the calculateAverageAndCheck function:
- Access each subject grade using the pointer.
- Add all three grades together to find the total score.
- Divide the total by
3.0to calculate the average grade. - Compare the result to the passing threshold of 60.
- If the average is greater than or equal to 60, return
true(the student passed). - Otherwise, return
false(the student failed).
Example
| Math | Physics | Programming | Average | Result |
|---|---|---|---|---|
| 85.0 | 55.0 | 60.0 | 66.67 | β Passed |
| 50.0 | 40.0 | 45.0 | 45.00 | β Failed |
| 60.0 | 60.0 | 60.0 | 60.00 | β Passed |
Solution
Thanks for your feedback!
single