A struct groups variables of different types under one name.
- Define:
struct Student { char name[30]; int marks; }; - Access members with a dot:
s.marks. - Through a pointer, use an arrow:
p->marks. typedefcreates a shorter type name.
Group related data with struct.
✍️ 2 exercises in this lessonA struct groups variables of different types under one name.
struct Student { char name[30]; int marks; };s.marks.p->marks.typedef creates a shorter type name.Mastering C Structures helps you write clean and reliable C code. Follow standard conventions, test edge cases, and keep logic simple. Try running the code example below and complete the exercises to build confidence.
#include <stdio.h>
#include <string.h>
typedef struct {
char name[30];
int marks;
} Student;
void bonus(Student *s) { s->marks += 5; }
int main() {
Student list[] = {{"Ali", 81}, {"Sara", 94}};
strcpy(list[0].name, "Ali Khan");
bonus(&list[0]);
for (int i = 0; i < 2; i++) {
printf("%s: %d\n", list[i].name, list[i].marks);
}
return 0;
}
Ali Khan: 86 Sara: 94
Check what you learned in “C Structures”.
Access a member through a pointer to a struct.
pmarks = 90;
Which keyword creates a shorter name for a type?
Stuck on something?
Ask the community in the CodesInTech forum.
Need a developer?
Learning on your own is great — and when you need professional help, our team builds custom software, websites and mobile apps for clients worldwide.
Explore Our Services