01/10/2018, 10:53

Hỏi về cấu trúc struct?

Cách 1;

struct node
{ 
      int key;
     node*ref
};

Cách 2;

typedef struct node*ref;
struct node{
int key;
ref next;
};

1.Cho mình hỏi sự khác biệt của 2 cách trên ?
2.Nếu mình khai báo thêm ref head=NULL thì nó sẽ trỏ vào đâu ?
Cảm ơn các bạn ^^

rogp10 viết 12:58 ngày 01/10/2018
  1. Cách thứ hai thì có thêm dòng typedef để abstract away.
    Thực ra cách 2 đúng hơn (ko ai gọi thành phần đó là ref cả).

  2. Vào NULL

BOYKAV viết 12:56 ngày 01/10/2018

trả lời như thế ai mà hiểu bạn

Tao Không Ngu. viết 12:57 ngày 01/10/2018

Hi Taivuong95.
Bạn tim hiểu thêm về từ khóa tyoedef rồi lên đây hỏi tiếp.

cdxf viết 13:07 ngày 01/10/2018

en.wikipedia.org

typedef

typedef is a reserved keyword in the C and C++ programming languages. It is used to create an alias name for another data type. As such, it is often used to simplify the syntax of declaring complex data structures consisting of struct and union types, but is just as common in providing specific descriptive type names for integer data types of varying lengths. The C standard library and POSIX reserve the suffix '_t', for example as in size_t and time_t. The syntax of the typedef declaration is: ...


Trong C:
Nếu không dùng typedef thì muốn khai báo biến với struct đó thì dùng:
struct MyStruct a;
Để giản lược từ khóa struct thì thêm Typedef:
typedef struct MyStruct newtype;
=> newtype a;
Trong C++: có thể khai báo biến mà không cần từ khóa struct phía trước, vì vậy cũng không cần thiết phải dùng typedef:
MyStruct a;

Bài liên quan
0