Structs and typedef: . vs ->
This page needs a recent browser (with SharedArrayBuffer support). Please update Chrome, Edge, Firefox or Safari to the latest version. · Trang này cần trình duyệt gần đây (hỗ trợ SharedArrayBuffer). Vui lòng cập nhật Chrome, Edge, Firefox hoặc Safari lên phiên bản mới nhất.
English
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.
Tiếng Việt
Nhóm dữ liệu liên quan
- Một struct gộp nhiều giá trị vào một thực thể. Một điểm có một
xvà mộty; một hình chữ nhật có mộtwidthvà mộtheight. - Mỗi giá trị bên trong được gọi là thành viên (hoặc trường). Chúng có thể có các kiểu khác nhau.
- Structs giúp bạn truyền một thực thể gọn gàng thay vì nhiều biến rời rạc.
English
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.
Tiếng Việt
struct và typedef
- Bạn chỉ định định dạng một lần:
struct { int x; int y; }có hai thành viênint. typedefgán cho định dạng đó một tên ngắn. Sau khitypedef struct { int x; int y; } Point;, bạn có thể viếtPoint p;.- Trong các bài tập này,
typedefđã được viết sẵn cho bạn trong file khởi tạo.
English
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.
Tiếng Việt
Truy cập thành viên với dấu chấm
- Đối với một giá trị struct, sử dụng dấu chấm:
p.xlà thành viênxcủap. - Bạn đọc nó (
int a = p.x;) và ghi nó (p.y = 5;) giống hệt như một biến thông thường. - Một hàm có thể xây dựng một struct, điền đầy các thành viên của nó, và
returntoàn bộ cấu trúc.
English
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).
Tiếng Việt
Thông qua con trỏ với dấu mũi tên
- Khi bạn có một con trỏ đến một struct, dùng dấu mũi tên:
p->xcó nghĩa là "thành viênxcủa đối tượng màptrỏ tới". p->xchỉ là cách viết tắt của(*p).x. Dấu mũi tên dễ đọc hơn.- Truyền một
Point *cho phép một hàm thay đổi struct của người gọi (giống như truyền theo con trỏ cho một giá trị).
#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;
}
English
Common mistakes
- Use
.on a struct value and->on a struct pointer. typedeflets you drop the wordstructwhen declaring.
Tiếng Việt
Lỗi thường gặp
- Sử dụng
.trên giá trị struct và->trên con trỏ struct. typedefcho phép bạn bỏ qua từstructkhi khai báo.
English
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.
Tiếng Việt
Bây giờ bạn thử
- Sử dụng
.trên giá trị struct, và->khi bạn có con trỏ đến một struct. typedefđược cung cấp trong mỗi file khởi tạo. Không viết mộtmain— trình kiểm tra sẽ tự làm điều đó.
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.
Click Run to see the output here. · Nhấn Chạy để xem kết quả ở đây.
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.
Click Run to see the output here. · Nhấn Chạy để xem kết quả ở đây.
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.
Click Run to see the output here. · Nhấn Chạy để xem kết quả ở đây.