Structs and typedef: . vs -> · 结构体与 typedef:. 与 ->
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.
把相关的数据组合起来
- 结构体把几个值组合成一个东西。一个点有
x和y;一个矩形有width和height。 - 里面的每个值叫做成员(或字段)。它们可以是不同的类型。
- 结构体让你把一个整齐的东西传来传去,而不是一堆零散的变量。
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 与 typedef
- 你先定义一次形状:
struct { int x; int y; }有两个int成员。 typedef给这个形状起一个简短的名字。写了typedef struct { int x; int y; } Point;之后,你就能写Point p;。- 在这些任务里,
typedef已经在起始代码里替你写好了。
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.
用 . 访问成员
- 对一个结构体值,用点号:
p.x是p的x成员。 - 你可以像普通变量一样读它(
int a = p.x;)和写它(p.y = 5;)。 - 一个函数可以构造一个结构体、填好成员,再
return整个结构体。
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).
用 -> 通过指针访问
- 当你有一个指向结构体的指针时,用箭头:
p->x表示“p所指向的那个东西的x”。 p->x只是(*p).x的简写。箭头更好读。- 传入一个
Point *能让函数修改调用者的结构体(就像对单个值的按指针传参)。
#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.
常见错误
- 对结构体值用
.,对结构体指针用->。 typedef让你声明时省去struct。
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.
现在轮到你了
- 对结构体值用
.,有指向结构体的指针时用->。 - 每个起始代码里都给了
typedef。不要自己写main—— 检查器会提供。
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 · 不 write a main. · 完成 Point make_point(int x, int y),让它用这两个值构造一个 Point 并返回(用 . 访问成员)。typedef 已给出。不要写 main。
Click Run to see the output here. · 点击“运行”查看此处输出。
Complete void move_point(Point *p, int dx, int dy) so it adds dx to the point's x and dy to its y, using ->. The typedef is given. Do not · 不 write a main. · 完成 void move_point(Point *p, int dx, int dy),用 -> 把 dx 加到点的 x 上、dy 加到 y 上。typedef 已给出。不要写 main。
Click Run to see the output here. · 点击“运行”查看此处输出。
Complete int rect_area(const Rect *r) so it returns width * height, reading the members with ->. The typedef is given. Do not · 不 write a main. · 完成 int rect_area(const Rect *r),让它返回 width * height,用 -> 读取成员。typedef 已给出。不要写 main。
Click Run to see the output here. · 点击“运行”查看此处输出。