Structs and typedef: . vs -> · Structs y 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.
Agrupar datos relacionados
- Un struct agrupa varios valores en una sola entidad. Un punto tiene
xey; un rectángulo tienewidthyheight. - Cada valor interno se llama miembro (o campo). Pueden tener diferentes tipos.
- Los structs permiten pasar una cosa ordenada en lugar de muchas variables sueltas.
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 y typedef
- Defines la forma una vez:
struct { int x; int y; }tiene dos miembrosint. typedefle da un nombre corto a esa forma. Después detypedef struct { int x; int y; } Point;, puedes escribirPoint p;.- En estas tareas, el
typedefya está escrito para ti en el código inicial.
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.
Acceso a miembros con .
- Para un valor de struct, usa un punto:
p.xes el miembroxdep. - Lo lees (
int a = p.x;) y lo escribes (p.y = 5;) igual que una variable normal. - Una función puede construir un struct, rellenar sus miembros y
returnel completo.
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).
A través de un puntero con ->
- Cuando tienes un puntero a un struct, usa una flecha:
p->xsignifica "elxde lo quepapunta". p->xes simplemente una abreviatura de(*p).x. La flecha es más fácil de leer.- Pasar un
Point *permite a una función modificar el struct del llamador (como pasar por puntero para un solo valor).
#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.
Errores comunes
- Usa
.en un valor de struct y->en un puntero a struct. typedefte permite omitir la palabrastructal declarar.
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.
Ahora tú intentas
- Usa
.en un valor de struct, y->cuando tengas un puntero a un struct. - El
typedefse proporciona en cada código inicial. No escribas unmain— el comprobador proporciona uno.
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 · no write a main. · Completa Point make_point(int x, int y) para que construya un Point con esos valores y lo devuelva (usa . en los miembros). El typedef está dado. No escribas un main.
Click Run to see the output here. · Haz clic en Ejecutar para ver la salida aquí.
Complete void move_point(Point *p, int dx, int dy) so it adds dx to the point's x and · y dy to its y, using ->. The typedef is given. Do not · no write a main. · Completa void move_point(Point *p, int dx, int dy) para que sume dx a la x del punto y dy a su y, usando ->. El typedef está dado. No escribas un main.
Click Run to see the output here. · Haz clic en Ejecutar para ver la salida aquí.
Complete int rect_area(const Rect *r) so it returns width * height, reading the members with ->. The typedef is given. Do not · no write a main. · Completa int rect_area(const Rect *r) para que devuelva width * height, leyendo los miembros con ->. El typedef está dado. No escribas un main.
Click Run to see the output here. · Haz clic en Ejecutar para ver la salida aquí.