结构体是 c 中 的数据类型
定义结构体 使用
struct 定义一个包含多个成员的新的数据类型
tag 是结构体标签。
member-list 是标准的变量定义,比如 int i; 或者 float f,或者其他有效的变量定义。
variable-list 结构变量,定义在结构的末尾,最后一个分号之前,可以指定一个或多个结构变量。
在一般情况下,tag、member-list、variable-list 这 3 部分至少要出现 2 个
struct character //人物
{
char character1[50];
char character2[50];
char character3[100];
int character4;
}charact;
另外两种定义方法
//此声明声明了拥有3个成员的结构体,分别为整型的a,字符型的b和双精度的c
//结构体的标签被命名为SIMPLE,没有声明变量
struct SIMPLE
{
int a;
char b;
double c;
};
//用SIMPLE标签的结构体,另外声明了变量t1、t2、t3
struct SIMPLE t1, t2[20], *t3;
//也可以用typedef创建新类型
typedef struct
{
int a;
char b;
double c;
} Simple2;
//现在可以用Simple2作为类型声明新的结构体变量
Simple2 u1, u2[20], *u3;
初始化结构体
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "test.h"
struct character //人物 类型
{
char character1[50];
char character2[50];
char character3[100];
char character4[100];
}charact={"一号","二号","三号","四号"};
int main(int argc, char *argv[]) {
printf("character1:%s\ncharacter2:%s\ncharacter3:%s\ncharacter4:%s\n",charact.character1,charact.character2,charact.character3,charact.character4);
system("pause");
return 0;
}
显示
访问结构成员 >>>> 结构的用法 >>>成员访问运算符(.)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "test.h"
struct character //人物 类型
{
char character1[50];
char character2[50];
char character3[100];
char character4[100];
};
int main(){
struct character a1; //声明 a1 ,类型为 characrter
struct character a2; //一个结构体 使用在结构上
//a1 详述
strcpy(a1.character1,"超人"); // strcpy 复制
strcpy(a1.character2,"巨人");
strcpy(a1.character3,"蝙蝠侠");
strcpy(a1.character4,"外星人");
//a2
strcpy(a2.character1,"一号");
strcpy(a2.character2,"二号");
strcpy(a2.character3,"三号");
strcpy(a2.character4,"四号");
//输出a1
printf("character1:%s\n",a1.character1);
printf("character2:%s\n",a1.character2);
printf("character3:%s\n",a1.character3);
printf("character4:%s\n",a1.character4);
//a2
printf("character1:%s\n",a2.character1);
printf("character2:%s\n",a2.character2);
printf("character3:%s\n",a2.character3);
printf("character4:%s\n",a2.character4); //成员访问运算符 .
system("pause");
return 0;
}
显示
结构作为函数传参
结构作为函数参数,传参方式与其他类型的变量或指针类似 ```cinclude
include
include
include “test.h”
struct character //人物 类型 { char character1[50]; char character2[50]; char character3[100]; char character4[100]; }; //声明 void printcharacter( struct character c1); int main(){
struct character a1; //声明 a1 ,类型为 characrter
struct character a2; //一个结构体 使用在结构上
//a1 详述
strcpy(a1.character1,"超人"); // strcpy 复制
strcpy(a1.character2,"巨人");
strcpy(a1.character3,"蝙蝠侠");
strcpy(a1.character4,"外星人");
//a2
strcpy(a2.character1,"一号");
strcpy(a2.character2,"二号");
strcpy(a2.character3,"三号");
strcpy(a2.character4,"四号");
printcharacter(a1);
printcharacter(a2);
system("pause");
return 0;
} void printcharacter( struct character c1) //(定义 struct 结构体 新的类型) { printf(“character1:%s\n”,c1.character1); printf(“character2:%s\n”,c1.character2); printf(“character3:%s\n”,c1.character3); printf(“character4:%s\n”,c1.character4);
}
指向结构的指针<br /> 定义指针 :struct character *c1;<br /> 指针变量中存储结构变量的地址: c1 = &a1;<br /> 为了使用指向该结构的指针访问结构的成员,您必须使用 -> 运算符 :c1->character1;
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "test.h"
struct character //人物 类型
{
char character1[50];
char character2[50];
char character3[100];
char character4[100];
};
//声明
void printcharacter( struct character *c1);
int main(){
struct character a1; //声明 a1 ,类型为 characrter
struct character a2; //一个结构体 使用在结构上
//a1 详述
strcpy(a1.character1,"超人"); // strcpy 复制
strcpy(a1.character2,"巨人");
strcpy(a1.character3,"蝙蝠侠");
strcpy(a1.character4,"外星人");
//a2
strcpy(a2.character1,"一号");
strcpy(a2.character2,"二号");
strcpy(a2.character3,"三号");
strcpy(a2.character4,"四号");
printcharacter(&a1);
printcharacter(&a2);
system("pause");
return 0;
}
void printcharacter( struct character *c1) //(定义 struct 结构体 新的类型)
{
printf("character1:%s\n",c1->character1);
printf("character2:%s\n",c1->character2);
printf("character3:%s\n",c1->character3);
printf("character4:%s\n",c1->character4);
}