在声明结构时,必须列出它包含的所有成员,这个列表包含每个成员的类型和名字。
struct tag { membler-liset } varibale-lists;
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
int main()
{
struct SIMPLE {
int a;
char b;
float c;
};
struct SIMPLE x;
struct SIMPLE y[20],*z; //现在它们是同一种类型了。
z = &x;
}
若果没有标签字段,则会被当做截然不同的字段。
或者,使用 typedef 创建一种新的类型。
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
int main()
{
typedef struct {
int a;
char b;
float c;
} SIMPLE;
SIMPLE x;
SIMPLE y[20], *z;
}
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
int main()
{
struct SIMPLE {
int a;
char b;
float c;
};
struct COMPLEX {
float f;
int a[20];
long *lp;
struct SIMPLE s;
struct SIMPLE sa[10];
struct SIMPLE *sp;
};
struct COMPLEX comp;
comp.sa[4].c;
struct SELF_REF1 {
int a;
struct SELF_REF1 *b;
int c;
};
typedef struct SELF_REF3_TAT {
int a;
struct SELF_REF3_TAG *b;
int c;
} SELF_REF3;
struct B;
struct A {
struct B *x; // 必须以指针的形式存在。
};
struct B {
struct A y;
};
}