用户定义数据类型

来源:互联网 发布:macbook 自带软件 编辑:程序博客网 时间:2024/06/05 03:05

1结构体:

实际变成中,处理批量数据是非常普遍的,对于同类型的批量数据;作为一个整体;

例如定义一结构体描述学生的类型

struct Student

{

    char name[9];

    unsigned No;

    float c;

    float ave;

};

关键字struct表示定义的是结构体数据结构类型,标识符Student是结构体类型名;


2typedef命令使用

用typedef来定义与平台无关的类型。

定义一种类型的别名,而不只是简单的宏替换。可以用作同时声明指针型的多个对象


3结构体指针

结构体指针变量是一个用来存放指向结构体变量的指针变量该指针变量的值就是它指向的结构体变量的起始地址

struct 结构体名*结构体指针变量名

#include<stdio.h>struct object{    char name[10];    float high;    float weight;};int main(){    struct object a={"first",1.73,74.2};    struct object *p=&a;    printf("%-10s%6.2f%6.2f\n",a.name,a.high,a.weight);    printf("%-10s%6.2f%6.2f\n",(*p).name,(*p).high,(*p).weight);    printf("%-10s%6.2f%6.2f\n",p->name,p->high,p->weight);    return 0;}


1 0