欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C 开发学习 - 结构类型(枚举/结构/类型定义)

程序员文章站 2022-03-23 20:07:03
一、枚举 定义:枚举是 一种用户定义的数据类型,它用的关键字 enum 枚举类型名字通常并不真的使用,要用的是在大括号里地名字,因为它们就是常量符合,它们的类型是int,值则依次...

一、枚举

定义:枚举是 一种用户定义的数据类型,它用的关键字 enum 枚举类型名字通常并不真的使用,要用的是在大括号里地名字,因为它们就是常量符合,它们的类型是int,值则依次从0到n。 enum colors {red, yellow, green} 语法:enum 枚举类型名称{名字0m, ..., 名字n};
案例一:自动计数的枚举
//
//  main.c
//  enum
//
//  Created by liuxinming on 15/4/26.
//  Copyright (c) 2015年 liuxinming. All rights reserved.
//

#include 

enum COLOR {RED, YELLOW, GREEN, NumCOLORS};

int main(int argc, const char * argv[]) {
    int color = -1;
    char *ColorNames[NumCOLORS] = {
        red, yellow, green
    };
    char *colorName = NULL;
    
    printf(输入你喜欢的颜色代码:);
    scanf(%d, &color);
    
    if(color >= 0 && color < NumCOLORS){
        colorName = ColorNames[color];
    }else{
        colorName = unknown;
    }
    
    
    printf(你喜欢的颜色是%s
, colorName);
    return 0;
}

案例二:枚举量 声明枚举量的时候可以指定值,enum COLOR{RED=1, YELLOW, GREEN=5}

二、结构

结构是由基本数据类型构成的、并用一个标识符来命名的各种变量的组合。
结构中可以使用不同的数据类型。

申明结构的形式

struct point{
  int x;
  int y;
}

struct point p1, p2; #p1和p2都是point,里面有x和y的值

struct {
  int x;
  int y;
}p1, p2;
#p1和p2都是一种无名结构,里面有x和y的值

结构变量

struct point p; #p就是一个结构变量
p.x = 12;
p.y = 20;

案例一:使用结构体

//
//  main.c
//  structure
//
//  Created by liuxinming on 15/4/12.
//  Copyright (c) 2015年 liuxinming. All rights reserved.
//

#include 

//声明结构类型
struct date{
    int month;
    int day;
    int year;
};


int main(int argc, const char * argv[]) {
    
    //结构变量&使用
    struct date today;
    today.month = 04;
    today.day = 12;
    today.year = 2015;
    
    printf(Today is date is  %i-%i-%i.
,today.year, today.month, today.day);
    
    
    return 0;
}


案列二:结构的初始化

//
//  main.c
//  structure
//
//  Created by liuxinming on 15/4/12.
//  Copyright (c) 2015年 liuxinming. All rights reserved.
//

#include 

//声明结构类型
struct date{
    int month;
    int day;
    int year;
};


int main(int argc, const char * argv[]) {
    
    struct date today = {04, 26, 2015};
    struct date thismonty = {.month = 4, .year = 2015};
    
    printf(Today is date is  %i-%i-%i.
,today.year, today.month, today.day);
    printf(This month is  %i-%i-%i.
, thismonty.year, thismonty.month, thismonty.day);
    
    return 0;
}

输出:
Today is date is 2015-4-26.
This month is 2015-4-0.

结构成员

* 结构和数组有点像 【数组里有很多单元,结构里有很多成员 * 数组用[]运算符和下标访问其成员 a[0] = 10; * 结构用.运算符和名字访问其成员 today.day

结构运算

* 要访问整个结构,直接用结构变量的名字 * 对于整个结构,可以做赋值、取地址、也可以传递给函数参数 p1 = (struct point) {5, 10} // 相当于p1.x = 5 p1.y = 10; p1 = p2; // 相当于p1.x = p2.x ; p1.y = p2.y;

结构指针

* 和数组不同,结构变量的名字并不是结构变量的地址,必须使用&运算符 * struct date *pDate = &today;
//
//  main.c
//  structure
//
//  Created by liuxinming on 15/4/12.
//  Copyright (c) 2015年 liuxinming. All rights reserved.
//

#include 

//声明结构类型
struct date{
    int month;
    int day;
    int year;
};


int main(int argc, const char * argv[]) {
    
    struct date today;
    
    today = (struct date){04, 26, 2015};
    
    struct date day;
    
    struct date *pDate = &today;
    
    printf(Today's date is %i-%i-%i.
, today.year,today.month,today.day);
    printf(The day's date is %i-%i-%i.
, day.year, day.month, day.day);
    printf(address of today is %p
, pDate);
    
    return 0;
}
输出:
Today's date is 2015-4-26.
The day's date is 0-1606416456-32767.
address of today is 0x7fff5fbff7f0
Program ended with exit code: 0

结构作为函数参数

int numberOfDays(struct date d) * 整个结构可以作为参数的值传入函数 * 这时候是在函数内新建一个结构变量,并复制调用者的结构的值 * 也可以返回一个结构 案例:输入今天日期 ,输出明天日期【主要介绍结构体用法,不具体说明实现过程,想了解的自己研究下】
//
//  main.c
//  structure
//
//  Created by liuxinming on 15/4/12.
//  Copyright (c) 2015年 liuxinming. All rights reserved.
//

#include 
#include 

//声明结构类型
struct date{
    int month;
    int day;
    int year;
};

bool isLeap(struct date d);//判断是否为闰年
int numberOfDays(struct date d);


int main(int argc, const char * argv[]) {
    
    struct date today, tomorrow;
    
    printf(Enter today's date(mm dd yyyy):);
    scanf(%i %i %i, &today.month, &today.day, &today.year);
    
    if(today.day != numberOfDays(today)){
        tomorrow.day = today.day + 1;
        tomorrow.month = today.month;
        tomorrow.year = today.year;
    } else if (today.month == 12){
        tomorrow.day = 1;
        tomorrow.month = 1;
        tomorrow.year = today.year + 1;
    } else{
        tomorrow.day = 1;
        tomorrow.month = today.month + 1;
        tomorrow.year = today.year;
    }
    printf(Tomorrow's date is %i-%i-%i.
, tomorrow.year, tomorrow.month, tomorrow.day);
    return 0;
}

int numberOfDays(struct date d){
    int days;
    const int daysPerMonth[12] = {31,28,31,30,31,30,31,31,30,31,30,31};
    
    if(d.month == 2 && isLeap(d)){
        days = 29;//闰年
    } else{
        days = daysPerMonth[d.month - 1];
    }
    
    return days;
}

bool isLeap(struct date d){
    bool leap = false;
    if ( (d.year % 4 == 0 && d.year % 100 != 0) || d.year % 400 == 0){
        leap = true;
    }
    return leap;
}

输出:
Enter today's date(mm dd yyyy):12 31 2014
Tomorrow's date is 2015-1-1.
Program ended with exit code: 0

指向结构的指针

struct date {
  int month;
  int day;
  int year;
} myday;

struct date *p = &myday;

(*p).month = 12;
p->month = 12;

* 用->表示指针所指向的结构变量中的成员
//
//  main.c
//  structure
//
//  Created by liuxinming on 15/4/12.
//  Copyright (c) 2015年 liuxinming. All rights reserved.
//

#include 

struct point {
    int x;
    int y;
};

struct point* getStruct(struct point*);
void output(struct point);
void print(const struct point *p);


int main(int argc, const char * argv[]) {
    struct point y = {0, 0};
    getStruct(&y);
    output(y);
    output(*getStruct(&y));
    print(getStruct(&y));
    return 0;
}

struct point* getStruct(struct point *p){
    scanf(%d, &p->x);
    scanf(%d, &p->y);
    printf(%d, %d
, p->x, p->y);
    return p;
}

void output(struct point p){
    printf(%d, %d
,p.x, p.y);
}

void print(const struct point *p){
    printf(%d, %d,p->x, p->y);
}

输出:
10
50
10, 50
10, 50

结构数组

struct date dates[100];

struct date dates[] = {
 {4,5,2005},
 {2,4,2005}
};

案例:
//
//  main.c
//  structure
//
//  Created by liuxinming on 15/4/12.
//  Copyright (c) 2015年 liuxinming. All rights reserved.
//

#include 

struct time{
    int hour;
    int minutes;
    int seconds;
};

struct time timeUpdate(struct time now);


int main(void) {
   
    struct time testTime[5] = {
        {11,59,59},
        {12,0,0},
        {1,29,29},
        {23,59,59},
        {19,12,27}
    };
    int i;
    
    for (i = 0; i < 5; i++) {
        printf(Time is %.2i:%.2i:%.2i
, testTime[i].hour,testTime[i].minutes, testTime[i].seconds);
        
        testTime[i] = timeUpdate(testTime[i]);
        
        printf(... one second later it's %.2i:%.2i:%.2i
, testTime[i].hour,testTime[i].minutes, testTime[i].seconds);
    }
    
    return 0;
}

struct time timeUpdate(struct time now){
    ++ now.seconds;
    if( now.seconds == 60){
        now.seconds = 0;
        ++ now.minutes;
        
        if (now.minutes == 60){
            now.minutes = 0;
            ++ now.hour;
            
            if(now.hour == 24){
                now.hour = 0;
            }
        }
    }
    
    return now;
}

输出:
Time is 11:59:59
... one second later it's 12:00:00
Time is 12:00:00
... one second later it's 12:00:01
Time is 01:29:29
... one second later it's 01:29:30
Time is 23:59:59
... one second later it's 00:00:00
Time is 19:12:27
... one second later it's 19:12:28
Program ended with exit code: 0

结构中的结构

struct dateAndTime{
  struct date sdate;
  struct time stime;
}

嵌套的结构

struct rectangle{
    struct point pt1;
    struct point pt2;
};

    //如果有变量
    struct rectangle r;
    //就可以有:
    r.pt1.x = 1;
    r.pt1.y = 2;
    r.pt2.x = 11;
    r.pt2.y = 22;
    
    //如果有变量定义
    struct rectangle *rp;
    rp = &r;
    //那么这四种形式是等价的
    //r.pt1.x , rp->pt1.x, (r.pt1).x, (rp->pt1).x
    //但是没有rp->pt1->x 因为pt1不是指针