struct
A data structure that groups different types of data together. By grouping related data such as names, ages, and scores, the structure of your program becomes clearer.
Syntax
// Defines a struct type.
struct TagName {
type member1;
type member2;
};
// Declares a struct variable.
struct TagName variableName;
// Declares and initializes at the same time.
struct TagName variableName = {value1, value2};
// Accesses a member.
variableName.memberName // Use the dot operator for variables
pointerVariable->memberName // Use the arrow operator for pointers
// Combine with typedef to omit the struct keyword.
typedef struct {
type memberName;
} TypeName;
Struct Operations Summary
| Operation | Syntax | Description |
|---|---|---|
| Member access | variable.memberName | Directly accesses a member of a struct variable. |
| Access via pointer | pointer->memberName | A shorthand for (*p).member. Use this when working with a struct through a pointer. |
| Initializer | {.memberName = value} | Designated initializers are available in C99 and later. |
| Struct assignment | s1 = s2 | Structs of the same type can be copy-assigned (all members are copied). |
| Struct size | sizeof(struct TagName) | Returns the total byte size of the struct. Padding may be inserted. |
Sample Code
#include <stdio.h>
#include <string.h>
// Defines a struct to represent a student.
typedef struct {
char name[50];
int age;
double score;
} Student;
// A function that takes a struct pointer and sets its values.
void init_student(Student *s, const char *name, int age, double score) {
strncpy(s->name, name, sizeof(s->name) - 1);
s->name[sizeof(s->name) - 1] = '\0';
s->age = age;
s->score = score;
}
// A function that prints the contents of a struct (passed by value).
void print_student(Student s) {
printf("Name: %s, Age: %d, Score: %.1f\n", s.name, s.age, s.score);
}
int main(void) {
// Declares and initializes a struct variable.
Student s1 = {"Taro", 20, 85.5};
print_student(s1); // Prints "Name: Taro, Age: 20, Score: 85.5".
// Accesses a member using the dot operator.
s1.score = 90.0;
printf("Updated score: %.1f\n", s1.score); // Prints "90.0".
// Operates via a pointer (using the arrow operator).
Student s2;
Student *p = &s2;
init_student(p, "Hanako", 21, 92.0);
printf("Name: %s\n", p->name); // Prints "Hanako".
printf("Score: %.1f\n", p->score); // Prints "92.0".
// Creates an array of structs.
Student class[3] = {
{"Taro", 20, 85.5},
{"Hanako", 21, 92.0},
{"Jiro", 19, 78.0},
};
for (int i = 0; i < 3; i++) {
print_student(class[i]);
}
// Checks the size (padding may be inserted).
printf("Size of Student: %zu bytes\n", sizeof(Student));
return 0;
}
Notes
When you pass a struct to a function, all its members are copied. For large structs, passing a pointer is more efficient to avoid unnecessary copying. If you receive a struct via a pointer, the original struct in the caller will be modified. If you do not want it modified, declare the parameter as const StructType *.
The memory size of a struct may not equal the simple sum of its member sizes, because the compiler can insert padding for alignment purposes. Use sizeof to check the exact size.
For sharing memory across multiple types, see union. For type aliases, see typedef.
If you find any errors or copyright issues, please contact us.