How to define struct in C Language

Source: Internet
Author: User

Original article address

1. Difference Between struct and typedef struct

Struct is the keyword of the struct. It is used to declare struct variables such
Struct student
{Char num [10];
Char name [20];
Int age;
};

Struct student stu [10] to declare a struct Array
-------------------------------------------------------------------------------
Typedef is used to define a new type name to replace an existing type name,
The above struct can be defined
Typedef struct student
{Char num [10];
Char name [20];
Int age;
} Stud;
That is to say, the original struct student is redefined as stud;

You can directly use stud stu [10] to declare a struct array.
2. Self-reference/mutual reference of struct

The self reference of a struct contains a pointer to its own struct.
Mutual reference indicates that multiple structs contain pointers pointing to other structs.

1. Self-reference struct
(1.1) When typedef is not used

Struct tag_1 {
Struct tag_1 A;/* struct */
Int value;
};
This declaration is wrong, because it is actually an infinite loop. Member B is a struct, and member B is a struct,

In turn, wireless loop.
This method is invalid because the length of the struct cannot be determined due to infinite nesting during memory allocation.
Correct Method: (Use Pointer ):
Struct tag_1 {
Struct tag_1 * A;/* pointer */
Int value;
};
Since the pointer length is determined (the pointer length is 4 on 32-bit machines), the compiler can determine the length of the struct.

(1.2) When typedef is used

Typedef struct {
Int value;
NODE * link;/* although pointers are also used, the problem here is: NODE is not yet defined */
} NODE;
The purpose here is to use typedef to create an alias NODEP for the struct.
However, this is incorrect because the scope of the type name starts from the end of the statement and cannot be used within the struct because it is not defined yet.

The correct method is as follows: There are three methods, with little difference. Which one can be used.

/* Method 1 */
Typedef struct tag_1 {
Int value;
Struct tag_1 * link;
} NODE;
/* Method 2 */
Struct tag_2;
Typedef struct tag_2 NODE;
Struct tag_2 {
Int value;
NODE * link;
};
/* Method 3 */
Struct tag_3 {
Int value;
Struct tag * link;
};
Typedef struct tag_3 NODE;
2. Mutual reference of struct
Incorrect method:
Typedef struct tag_a {
Int value;
B * bp;/* type B has not been defined */
};
Typedef struct tag_ B {
Int value;
A * ap;
} B; the cause of the error is the same as above. Here type B is used before its definition.

Correct Method: (use incomplete declaration ")

/* Method 1 */
Struct tag_a {
Struct tag_ B * bp;/* struct tag_ B is not defined yet, but the compiler can accept */
Int value;
};

Struct tag_ B {
Struct tag_a * ap;
Int value;
};

Typedef struct tag_a;
Typedef struct tag_ B B;

/* Method 2 */
Struct tag_a;/* use the incomplete declaration of struct (incomplete declaration), which is also true */
Struct tag_ B; // This sentence is omitted.
Typedef struct tag_a;
Typedef struct tag_ B B;
Struct tag_a {
Struct tag_ B * bp;/* struct tag_ B is not defined yet, but the compiler can accept */
Int value;
};
Struct tag_ B {
Struct tag_a * ap;
Int value;
};
========================================================== ==============================

2. a useful document on struct Declaration

[Original article address: http://hi.baidu.com/gubuntu/blog/item/70d8d16079535eda8cb10d8e.html]

Use in C ++:
Struct test
{
Int x, y;
};
A struct named test can be defined, but C may not be compiled.

The C language does not support defining the struct name using the identifier after struct. test will be ignored, which is equivalent to defining a struct without a name.

If a test mt object is defined for this struct, an undefined test error message is displayed.

Therefore, in C language, typedef is generally used to define struct. The preceding example can be changed:
Typedef struct _ test {
Int x, y;
} Test;
_ Test is optional.

In addition, the first braces cannot be written in line breaks (because they are typedef) as casually as they were originally ).

------------------------------------------------------------------------------
# Define S (s) printf ("% s \ n", # s); s

Typedef struct _ TS1 {

Int x, y;

} TS1, * PTS1, *** PPPTS1; // TS1 is the struct name, And PTS1 is the struct pointer name.

// Name the struct _ TS1 as ts1,

// Name struct _ TS1 * PTS1

// Name struct _ TS1 *** PPPTS1

Typedef struct {// The structure description after struct can also be removed

Int x, y;

} TS2, * PTS2;

Typedef PTS1 * PPTS1; // defines that PPTS1 is a pointer to PTS1.

Typedef struct _ TTS1 {

Typedef struct ITTS1 {

Int x, y;

} Iner;

Iner I;

Int x, y;

} TTS1;

// The struct can also be defined.

Typedef TTS1: ITTS1 ITS1;

Void test_struct ()

{

// Use the basic structure weight Definition

TS1 ts1 = {100,200 };

PTS1 pts1 = & ts1; // It is equivalent to TS1 * pts1 = & ts1;

PPTS1 ppts1 = & pts1; // It is equivalent to TS1 ** ppts1 = & pts1;

PPPTS1 pppts1 = & ppts1; // It is equivalent to TS1 *** pppts1 = & ppts1;

TS2 ts2 = {99, 88 };

PTS2 pts2 = & ts2; // It is equivalent to TS2 * pts2 = & ts2;

TTS1 itts1 ={{ 110,220}, 10, 20 };

Its1 * rits1 = & itts1. I;

ITS1 * & its1 = rits1; // equivalent to TTS1: ITTS1 * its1 = & (itts1. I );

Printf ("ts1 \ t = (% d, % d) \ n * pts1 \ t = (% d, % d) \ n"

"** Ppts1 \ t = (% d, % d) \ n *** pppts1 = (% d, % d) \ n ",

Ts1.x, ts1.y, pts1-> x, pts1-> y,

(** Ppts1). x, (** ppts1). y, (*** pppts1). x, (*** pppts1). y );

Printf ("ts2 \ t = (% d, % d) \ n * pts2 \ t = (% d, % d) \ n ",

Ts2.x, ts2.y, pts2-> x, pts2-> y );

Printf ("itts1 \ t = [(% d, % d), % d, % d] \ n * its1 \ t = (% d, % d) \ n ",

Itts1. I. x, itts1. I. y, itts1.x, itts1.y, its1-> x, its1-> y );

S (pts1-& gt; x = 119 );

S (pts2-& gt; y = 911 );

S (its1-> x = 999 );

Printf ("ts1 \ t = (% d, % d) \ n * pts1 \ t = (% d, % d) \ n"

"** Ppts1 \ t = (% d, % d) \ n *** pppts1 = (% d, % d) \ n ",

Ts1.x, ts1.y, pts1-> x, pts1-> y,

(** Ppts1). x, (** ppts1). y, (*** pppts1). x, (*** pppts1). y );

Printf ("ts2 \ t = (% d, % d) \ n * pts2 \ t = (% d, % d) \ n ",

Ts2.x, ts2.y, pts2-> x, pts2-> y );

Printf ("itts1 \ t = [(% d, % d), % d, % d] \ n * its1 \ t = (% d, % d) \ n ",

Itts1. I. x, itts1. I. y, itts1.x, itts1.y, its1-> x, its1-> y );

S (* ppts1)-& gt; y =-9999 );

Printf ("ts1 \ t = (% d, % d) \ n ** ppts1 \ t = (% d, % d) \ n ",

Ts1.x, ts1.y, (* ppts1)-> x, (* ppts1)-> y );

S (** pppts1)-> x =-12345 );

S (*** pppts1). y =-67890 );

Printf ("ts1 \ t = (% d, % d) \ n * pts1 \ t = (% d, % d) \ n"

"** Ppts1 \ t = (% d, % d) \ n *** pppts1 = (% d, % d) \ n ",

Ts1.x, ts1.y, pts1-> x, pts1-> y,

(** Ppts1). x, (** ppts1). y, (*** pppts1). x, (*** pppts1). y );

}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.