Structs and typedef: . vs -> · Structs e 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.
Agrupando dados relacionados
- Uma struct agrupa vários valores em uma única coisa. Um ponto tem um
xe umy; um retângulo tem umwidthe umheight. - Cada valor interno é chamado de membro (ou campo). Eles podem ter tipos diferentes.
- Structs permitem passar uma única coisa organizada em vez de muitas variáveis soltas.
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 e typedef
- Você define a forma uma vez:
struct { int x; int y; }tem doisintmembros. typedefdá um nome curto àquela forma. Depois detypedef struct { int x; int y; } Point;, você pode escreverPoint p;.- Nestas tarefas, o
typedefjá está escrito para você no starter.
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.
Acesso a membros com .
- Para um valor de struct, use um ponto:
p.xé o membroxdep. - Você o lê (
int a = p.x;) e o escreve (p.y = 5;) assim como uma variável normal. - Uma função pode construir uma struct, preencher seus membros e
returntudo isso.
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).
Através de um ponteiro com ->
- Quando você tem um ponteiro para uma struct, use uma seta:
p->xsignifica "oxdo quepaponta". p->xé apenas um atalho para(*p).x. A seta é mais fácil de ler.- Passar um
Point *permite que uma função modifique a struct do caller (como pass-by-pointer para um único 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.
Erros comuns
- Use
.em um valor de struct e->em um ponteiro para uma struct. typedefpermite omitir a palavrastructquando 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.
Agora você tenta
- Use
.em um valor de struct, e->quando tiver um ponteiro para uma struct. - O
typedefé fornecido em cada starter. Não escreva ummain— o checker fornece um.
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 · não write a main. · Complete Point make_point(int x, int y) para construir um Point com esses valores e retorná-lo (use . nos membros). O typedef é dado. Não escreva um main.
Click Run to see the output here. · Clique em Executar para ver a saída aqui.
Complete void move_point(Point *p, int dx, int dy) so it adds dx to the point's x and · e dy to its y, using ->. The typedef is given. Do not · não write a main. · Complete void move_point(Point *p, int dx, int dy) para adicionar dx ao x do ponto e dy ao seu y, usando ->. O typedef é dado. Não escreva um main.
Click Run to see the output here. · Clique em Executar para ver a saída aqui.
Complete int rect_area(const Rect *r) so it returns width * height, reading the members with ->. The typedef is given. Do not · não write a main. · Complete int rect_area(const Rect *r) para que retorne width * height, lendo os membros com ->. O typedef é dado. Não escreva um main.
Click Run to see the output here. · Clique em Executar para ver a saída aqui.