数据结构-->结构体

来源:互联网 发布:tensorflow教程pdf 编辑:程序博客网 时间:2024/06/16 11:48
  • 使用struct可以聚合不同数聚类性的数据,结构表示的是数据项的集合,每条数据项由其类型和名称指定;
struct {    char name[10];    int age;    float salary;}person;
  • 对于数据成员的赋值可以以采用.来为数据成员进行赋值,需要注意的是对于字符性赋值需要使用strcpy函数
    来进行;
  • 还可以使用typedef来重新声明一个新的类型,使用上面的方式构造的只是一个变量,使用typedef得到的是一个
    新的类型,新的类型声明之后,可以使用类型来定义变量,但是对于person就不可以用于定义变量;
#include<stdio.h>#include<stdlib.h>#include<string.h>#define true 1#define false 0struct {    char name[10];    int age;    float salary;}person;//同样的对于结构体来说,也是可以包含结构体的;typedef struct {    int month;    int day;    int year;}date;typedef struct{    char name[10];    int age;    float salary;    date dob;}HumanBeing;bool Compare_struct(HumanBeing person1,HumanBeing person2){    if(strcmp(person1.name,person2.name)&&person1.age==    person2.age&&person1.salary==person2.salary)        return true;    else        return false;}int main(){    strcpy(person.name,"Hello world");    person.age=10;    person.salary=12.255;    printf("name: %s\n",person.name);    printf("age: %d\n",person.age);    printf("salary: %f\n",person.salary);    HumanBeing myinfo;    strcpy(myinfo.name,"new name");    myinfo.age=10;    myinfo.salary=10.001;    myinfo.dob.year=2015;    myinfo.dob.month=11;    myinfo.dob.day=23;    HumanBeing person2;    //对于相同类型的结构体是可以进行赋值的,但是不可以进行比较操作;    myinfo=person2;}