A struct combines related values into one object. Every member has separate storage, so values of different types coexist. Use the dot operator to access a member of a struct object.
#include <stdio.h>
struct Student {
char name[32];
int score;
double attendance;
};
int main(void) {
struct Student student = {"Maya", 88, 0.96};
printf("%s: %d, %.0f%%\n",
student.name,
student.score,
student.attendance * 100.0);
return 0;
}Members appear in declaration order, but an implementation may add padding between members or at the end for alignment. Do not guess offsets; use sizeof when the size matters.
Separate members inside one object
The outer boundary is one student variable. Change a member and watch the other members retain their own data inside that same object.
All three values exist inside student at the same time. Run any statement: only its member's region changes and is highlighted.
Unlike a union, these regions do not overlap. Widths are not to byte scale; name's 32 character positions and possible padding are not expanded. Use sizeof(student) for the actual total size. strcpy requires <string.h>; both names fit in the name array.Arrays of structs
A struct type can be an array element type. Every element is a complete, independent Student object, and a member is accessed with an expression such as students[i].score.
#include <stdio.h>
struct Student {
char name[32];
int score;
double attendance;
};
int main(void) {
struct Student students[] = {
{"Maya", 88, 0.96},
{"Noah", 93, 0.91},
{"Iris", 85, 0.98}
};
int count = (int) (sizeof(students) / sizeof(students[0]));
for (int i = 0; i < count; i++) {
printf("%s %d\n", students[i].name, students[i].score);
}
return 0;
}Passing a struct through a pointer
Passing a struct by value copies the complete object. Passing its address avoids that copy and lets a function modify the original. For a struct pointer, student->score is equivalent to (*student).score.
#include <stdio.h>
struct Student {
char name[32];
int score;
double attendance;
};
void add_bonus(struct Student *student, int points) {
if (student != NULL) {
student->score += points;
}
}
int main(void) {
struct Student student = {"Maya", 88, 0.96};
add_bonus(&student, 5);
printf("%d\n", student.score);
return 0;
}KNOWLEDGE QUIZ
KNOWLEDGE QUIZ
KNOWLEDGE QUIZ
Define and print a book
Define Book with char title[40], int pages, and double price. Create an object containing “C Handbook,” 320, and 39.90, then print C Handbook | 320 pages | $39.90.
- Declare Book with title, author, and price members.
- Initialize one Book value with sufficient character-array capacity.
- Read members with the dot operator and format the price.
Find the highest-scoring student
Define Student with name and score. Implement const struct Student *top_student(const struct Student *students, int count), returning the address of the highest-scoring element or NULL when count is not positive. Test with three students.
- Define Student with name and score members.
- Initialize the best index from the first student.
- Scan remaining elements, update the index for a higher score, and print that member.
Update inventory through a pointer
Define Product with name and stock. Implement int sell(struct Product *product, int amount): return 0 for a NULL pointer, a nonpositive amount, or insufficient stock; otherwise reduce stock and return 1. Starting from 8, attempt sales of 3 and 6 and finish with stock 5.
- Define Inventory with an item name and quantity.
- Validate the pointer and amount first.
- Use -> to update the original quantity and return an operation status.