Structs and typedef: . vs -> · Structures et typedef : . vs ->
Grouping related data
- A struct groups several values into one thing. A point has an
xand ay; a rectangle has awidthand aheight. - Each value inside is called a member (or field). They can have different types.
- Structs let you pass one tidy thing around instead of many loose variables.
Regrouper des données liées
- Un struct regroupe plusieurs valeurs en une seule chose. Un point a un
xet uny; un rectangle a unwidthet unheight. - Chaque valeur à l'intérieur est appelée un membre (ou champ). Ils peuvent avoir différents types.
- Les structs permettent de passer une seule chose propre autour au lieu de nombreuses variables dispersées.
struct and typedef
- You define the shape once:
struct { int x; int y; }has twointmembers. typedefgives that shape a short name. Aftertypedef struct { int x; int y; } Point;, you can writePoint p;.- In these tasks, the
typedefis already written for you in the starter.
struct et typedef
- Vous définissez la forme une fois :
struct { int x; int y; }a deux membresint. typedefdonne un nom court à cette forme. Aprèstypedef struct { int x; int y; } Point;, vous pouvez écrirePoint p;.- Dans ces exercices, le
typedefest déjà écrit pour vous dans le modèle de départ.
Member access with .
- For a struct value, use a dot:
p.xis thexmember ofp. - You read it (
int a = p.x;) and write it (p.y = 5;) just like a normal variable. - A function can build a struct, fill its members, and
returnthe whole thing.
Accès aux membres avec .
- Pour une valeur de struct, utilisez un point :
p.xest le membrexdep. - Vous le lisez (
int a = p.x;) et l'écrivez (p.y = 5;) comme une variable normale. - Une fonction peut construire un struct, remplir ses membres, et
returnl'ensemble.
Through a pointer with ->
- When you have a pointer to a struct, use an arrow:
p->xmeans "thexof whatppoints to". p->xis just shorthand for(*p).x. The arrow is easier to read.- Passing a
Point *lets a function change the caller's struct (like pass-by-pointer for one value).
À travers un pointeur avec ->
- Quand vous avez un pointeur vers un struct, utilisez une flèche :
p->xsignifie "lexde ce queppointe". p->xest simplement une abréviation pour(*p).x. La flèche est plus facile à lire.- Passer un
Point *permet à une fonction de modifier le struct de l'appelant (comme le passage par pointeur pour une valeur).
#include <stdio.h>
typedef struct {
int x;
int y;
} Point;
int main(void) {
Point p = {3, 4};
Point *ptr = &p;
ptr->x = 10; // changes p.x through the pointer
printf("%d %d\n", p.x, p.y); // 10 4
return 0;
}
Common mistakes
- Use
.on a struct value and->on a struct pointer. typedeflets you drop the wordstructwhen declaring.
Erreurs courantes
- Utilisez
.sur une valeur de struct et->sur un pointeur de struct. typedefvous permet d'omettre le motstructlors de la déclaration.
Now you try
- Use
.on a struct value, and->when you have a pointer to a struct. - The
typedefis given in each starter. Do not write amain— the checker provides one.
À vous maintenant
- Utilisez
.sur une valeur de struct, et->quand vous avez un pointeur vers un struct. - Le
typedefest donné dans chaque modèle de départ. Ne pas écrire unmain— le vérificateur en fournit un.
Complete Point make_point(int x, int y) so it builds a Point with those values and returns it (use . on the members). The typedef is given. Do not · non write a main. · Complétez Point make_point(int x, int y) pour qu'il construise un Point avec ces valeurs et le retourne (utilisez . sur les membres). Le typedef est donné. Ne pas écrire de main.
Click Run to see the output here. · Cliquez sur Exécuter pour voir le résultat ici.
Complete void move_point(Point *p, int dx, int dy) so it adds dx to the point's x and · et dy to its y, using ->. The typedef is given. Do not · non write a main. · Complétez void move_point(Point *p, int dx, int dy) de sorte qu'elle ajoute dx à x du point et dy à son y, en utilisant ->. Le typedef est fourni. Ne n'écrivez pas un · non main.
Click Run to see the output here. · Cliquez sur Exécuter pour voir le résultat ici.
Complete int rect_area(const Rect *r) so it returns width * height, reading the members with ->. The typedef is given. Do not · non write a main. · Complétez int rect_area(const Rect *r) pour qu'il retourne width * height, en lisant les membres avec ->. Le typedef est donné. Ne pas écrire de main.
Click Run to see the output here. · Cliquez sur Exécuter pour voir le résultat ici.