C struct: Define and Use Structures
A struct groups values that go together, such as a name and a score, into one new type. The values can have different types, and you use a value inside it (a member) with variable.member.
struct Name {
type member1;
type member2;
};
struct Name variable = {value1, value2};
Defining a struct and using its members
struct Student { ... }; defines the layout (with a semicolon at the end), and struct Student s makes a variable. Inside { } you can write the starting values in member order, or point at a member by name, as in .score = 70.
#include <stdio.h> struct Student { char name[20]; int score;}; int main(void) { struct Student s = {"Mina", 90}; struct Student t = {.score = 70, .name = "Jun"}; s.score += 5; printf("%s %d\n", s.name, s.score); printf("%s %d\n", t.name, t.score); return 0;}Mina 95 Jun 70
Shorter names with typedef, and assignment copies everything
If you define it as typedef struct { ... } Size;, you can write just Size without struct. Unlike an array, a struct can be assigned with =, and all its members are copied. Passing one to a function and returning one are copies too.
#include <stdio.h> typedef struct { int w, h;} Size; Size doubled(Size s) { s.w *= 2; s.h *= 2; return s;} int main(void) { Size a = {2, 3}; Size b = doubled(a); printf("%d %d / %d %d\n", a.w, a.h, b.w, b.h); return 0;}2 3 / 4 6
The s inside doubled is a copy of a, so a is still 2 3 even though the function changes s.
Pointers and the arrow ->
To change the original from a function, pass the address of the struct. To reach a member through a pointer you have to write (*h).hp, and h->hp is the short form of that. You also use a pointer to pass a large struct without copying it.
#include <stdio.h> typedef struct { char name[20]; int hp;} Hero; void hit(Hero *h, int damage) { h->hp -= damage; if (h->hp < 0) h->hp = 0;} int main(void) { Hero k = {"Kai", 30}; hit(&k, 12); hit(&k, 25); printf("%s %d\n", k.name, k.hp); return 0;}Kai 0
Use a dot with a variable (k.hp) and an arrow with a pointer (h->hp).
Arrays of structs
A struct is a type too, so you can make an array of it. Pick one element by index, then pick a member with a dot, as in list[i].score.
#include <stdio.h> typedef struct { char name[20]; int score;} Player; int main(void) { Player list[3] = {{"Ara", 72}, {"Bin", 95}, {"Cho", 88}}; int best = 0; for (int i = 1; i < 3; i++) { if (list[i].score > list[best].score) best = i; } printf("%s %d\n", list[best].name, list[best].score); return 0;}Bin 95
Common mistakes
Passing by value and expecting the original to change
#include <stdio.h>
typedef struct { int x, y; } Point;
void move_right(Point p) {
p.x += 10;
}
int main(void) {
Point p = {1, 2};
move_right(p);
printf("%d %d\n", p.x, p.y);
return 0;
}
→ Still 1 2, not 11 2
The function receives a copy. Change the parameter to Point *p, write p->x += 10;, and call it with move_right(&p);. Or return the changed struct and store it with p = move_right(p);.
Leaving out the semicolon at the end of the definition
struct Point {
int x, y;
} /* a ; is needed here */
int main(void) {
return 0;
}
→ It doesn't compile. The error points at the next line, not at the struct, so it is hard to find
Unlike the } of a function, a struct definition ends with };. If you get an error you can't explain on the line right below a struct, check this first.
Leaving out struct when there is no typedef
struct Point { int x, y; };
Point p; /* not allowed in C */
struct Point q; /* write it this way */
→ Point p; doesn't compile in C (it does in C++)
In C the type name is the whole struct Point. For a short name, define it with typedef struct { int x, y; } Point;.
Comparing structs with ==
Point a = {1, 2}, b = {1, 2};
if (a == b) { } /* structs have no == */
→ It doesn't compile
Assignment (=) works, but comparison doesn't. Compare member by member, as in a.x == b.x && a.y == b.y. Compare string members with strcmp.