程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> 關於C語言 >> C 開發學習 - 結構類型(枚舉/結構/類型定義)

C 開發學習 - 結構類型(枚舉/結構/類型定義)

編輯:關於C語言

C 開發學習 - 結構類型(枚舉/結構/類型定義)


一、枚舉

定義:枚舉是 一種用戶定義的數據類型,它用的關鍵字 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不是指針




  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved