不明なsizeof(enum)データ構造ストレージの問題

62506 ワード

    sizeof(enum)        

typedef struct weekday_st { enum week {sun=123456789,mon,tue,wed,thu,fri,sat,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,aa,ab,ac,ad,ae,af,ag,ah,ai,aj,ak}; enum day{monring, moon, aftermoon}; }weekday_st; int main(int argc, char *argv[]) { printf("sizeof(weekday_st)=%d
", sizeof(weekday_st)); printf("sizeof(weekday)=%d
", sizeof(weekday_st::week)); printf("sizeof(day)=%d
", sizeof(weekday_st::day)); return 0; } sizeof(weekday_st)=1 sizeof(weekday)=4 sizeof(day)=4 sizeof 1, enum , , 4 ?


 emum 4
emum
2 emum

enum ,

printf("sizeof(weekday_st)=%d
", sizeof(weekday_st));

, sizeof 0

sizeof
PC char
sizeof 1

printf("sizeof(weekday)=%d
", sizeof(weekday_st::week));
printf("sizeof(day)=%d
", sizeof(weekday_st::day));

sizeof 4

, ,enum int

enum int , int *  , enum , sizeof 4, 。

typedef struct weekday_st
{
    enum week  {sun=123456789,mon,tue,wed,thu,fri,sat,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,aa,ab,ac,ad,ae,af,ag,ah,ai,aj,ak};
    enum day{monring, moon, aftermoon};
}weekday_st;
-----------------------------------------------------------------------------------
enum week enum day “ ”, , :
typedef struct weekday_st
{
    enum week  {sun=123456789,mon,tue,wed,thu,fri,sat,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,aa,ab,ac,ad,ae,af,ag,ah,ai,aj,ak} ew;
    enum day{monring, moon, aftermoon} ed;
}weekday_st;

sizeof , 8 。

, 8 , sizeof(week), week 4, ?
enum ...  UINT ...

, , 。
enum , “ ”。
struct 。
enum week ed; ed.mon 。

C++ Primer : enum , .: int,unsigned int,long ,unsigned long, ,sizeof(enum) 4






列挙体変数とそのメンバー定数のサイズについて(sizeof)
#include <stdio.h>

int main()

{

	enum foo{};

	enum gender{male, female};

	enum season{spring, summer, autumn, winter};

	enum week{sunday, monday, tuesday, wednesday, thursday, friday, saturday};

	printf("%d
",sizeof(enum));// ? 4 printf("%d
",sizeof(foo));// 4 printf("%d
",sizeof(gender));// 4 printf("%d
",sizeof(season));// 4 printf("%d
",sizeof(week));// 4 printf("%d
",sizeof(male));// 4 return 0;
}
       Visual C++ 6.0    。         4 ?





説明:列挙型は、前処理命令defineの代替である.列挙型は集合であり、集合内の要素(列挙メンバー)はいくつかの命名された整数定数であり、要素間はカンマで区切られている.タイプ定義はセミコロンです.終わります.最初の列挙メンバーのデフォルト値は整数の0で、後続の列挙メンバーの値は前のメンバーに1を加えます. 
連発科の筆記試験問題:
#include   
  • #include   

  • using namespace std;  
  •  

  • enum value  
  • {  

  •     value1,  
  •     value2,  

  •     value3=0,  
  •     value4=50,  

  •     value5,  
  • };  

  •  
  • void main()  

  • {  
  •     int i=1;  

  •  
  •     do{  

  •         cout<
  •         i++;  

  •         if(i<4)  
  •         continue;  

  •         break;  
  •     }while(value2);  

  •  
  • }  

  • 列挙とsizeof
    #include   
  •  

  • enum WEEK  
  • {  

  •     sat=2,  
  •     sun,  

  •     mon=11,  
  •     tue,      

  • };  
  •  

  • int main(void)  
  • {  

  •     printf("%d %d %d",sun,tue,sizeof(tue));   //3 12 4  
  •     printf("%d %d %d",sizeof(0),sizeof(WEEK),sizeof(sat)); //4 4 4  

  •     return 0;  

  • #include 
    enum 
  • {   

  •     BELL          = '\a',  
  •     BACKSPACE = '\b',  

  •     HTAB         = '\t',  
  •     RETURN      = '\r',  

  •     NEWLINE    = '',   
  •     VTAB         = '\v',  

  •     SPACE       = ' ' 
  • };  

  • enum BOOLEAN { FALSE = 0, TRUE } match_flag;  
    void main()  
    {  
  •    

  •     int index = 0;  
  •     int count_of_letter = 0;  

  •     int count_of_space = 0;  
        char str[] = "I'm Ely efod";  
        match_flag = FALSE;  
        for(; str[index] != '\0'; index++)  
  •         if( SPACE != str[index] )  

  •             count_of_letter++;  
  •         else 

  •         {  
  •             match_flag = (enum BOOLEAN) 1;  

  •             count_of_space++;  
  •         }  

  •       
  •     printf("%s %d times %c", match_flag ? "match" : "not match", count_of_space, NEWLINE);  

  •     printf("count of letters: %d %c%c", count_of_letter, NEWLINE, RETURN);  

  •  
     
     
     
     
    struct A
    {
      enum day{monring, moon, aftermoon};   
    };
    sizeof(A) //1
    sizeof(A::day) //4

    struct A
    {
      enum day{monring, moon, aftermoon} today;  
    };
    sizeof(A), 4
        ,enum day{monring, moon, aftermoon};       enum     ,                 ,  struct A              ,         1   


    sizeof(A) //     1
    sizeof(A::day) // day

     day, 。
    enum day , 。 。
    ,sizeof(A), ,
    C int,
    sizeof(A::day) 4.










    C - : VC++ 6.0 , , #define , : #define MON 1 #define TUE 2 #define WED 3 #define THU 4 #define FRI 5 #define SAT 6 #define SUN 7 , , 。 。 1. - - enum DAY { MON=1, TUE, WED, THU, FRI, SAT, SUN }; (1) , ( ) , , 。 (2) DAY , , , 。 (3) 0, 1。 (4) , 。 (5) #define 。 (6) ; 。 2. , 。 , : int, float, double, char, short 。 : char a; // a char char letter; int x, y, z; // x,y z int int number; double m, n; double result; // result double , 。 : enum DAY { MON=1, TUE, WED, THU, FRI, SAT, SUN }; enum DAY yesterday; enum DAY today; enum DAY tomorrow; // tomorrow enum DAY enum DAY good_day, bad_day; // good_day bad_day enum DAY : : enum // , DAY , 。 { saturday, sunday = 0, monday, tuesday, wednesday, thursday, friday } workday; // workday enum DAY enum week { Mon=1, Tue, Wed, Thu, Fri Sat, Sun} days; // days enum week enum BOOLEAN { false, true } end_flag, match_flag; // : typedef , : typedef enum workday { saturday, sunday = 0, monday, tuesday, wednesday, thursday, friday } workday; // workday enum workday workday today, tomorrow; // today tomorrow workday, enum workday enum workday workday : typedef enum { saturday, sunday = 0, monday, tuesday, wednesday, thursday, friday } workday; // workday enum workday workday today, tomorrow; // today tomorrow workday, enum workday : typedef enum workday { saturday, sunday = 0, monday, tuesday, wednesday, thursday, friday }; workday today, tomorrow; // today tomorrow workday, enum workday : , 。 : : typedef enum { wednesday, thursday, friday } workday; typedef enum WEEK { saturday, sunday = 0, monday, } workday; : typedef enum { wednesday, thursday, friday } workday_1; typedef enum WEEK { wednesday, sunday = 0, monday, } workday_2; 3. 3.1 。 : : , #include<stdio.h> /* */ enum DAY { MON=1, TUE, WED, THU, FRI, SAT, SUN }; void main() { /**/ int x, y, z; x = 10; y = 20; z = 30; /**/ enum DAY yesterday, today, tomorrow; yesterday = MON; today = TUE; tomorrow = WED; printf("%d %d %d
    ", yesterday, today, tomorrow); } : #include <stdio.h> /* */ enum DAY { MON=1, TUE, WED, THU, FRI, SAT, SUN }; void main() { /* */ int x=10, y=20, z=30; /* */ enum DAY yesterday = MON, today = TUE, tomorrow = WED; printf("%d %d %d
    ", yesterday, today, tomorrow); } : , 。 #include <stdio.h> /* , , */ enum DAY { MON=1, TUE, WED, THU, FRI, SAT, SUN } yesterday, today, tomorrow; /**/ int x, y, z; void main() { /* */ x = 10; y = 20; z = 30; /* */ yesterday = MON; today = TUE; tomorrow = WED; printf("%d %d %d
    ", x, y, z); // :10 20 30 printf("%d %d %d
    ", yesterday, today, tomorrow); // :1 2 3 } : , , 。 #include <stdio.h> /* , , 。 */ enum DAY { MON=1, TUE, WED, THU, FRI, SAT, SUN } yesterday = MON, today = TUE, tomorrow = WED; /* , 。 */ int x = 10, y = 20, z = 30; void main() { printf("%d %d %d
    ", x, y, z); // :10 20 30 printf("%d %d %d
    ", yesterday, today, tomorrow); // :1 2 3 } 3.2 , 。 #include <stdio.h> enum DAY { MON=1, TUE, WED, THU, FRI, SAT, SUN }; void main() { enum DAY yesterday, today, tomorrow; yesterday = TUE; today = (enum DAY) (yesterday + 1); // tomorrow = (enum DAY) 30; // //tomorrow = 3; // printf("%d %d %d
    ", yesterday, today, tomorrow); // :2 3 30 } 3.3 #include<stdio.h> enum { BELL = '\a', BACKSPACE = '\b', HTAB = '\t', RETURN = '\r', NEWLINE = '
    ', VTAB = '\v', SPACE = ' ' }; enum BOOLEAN { FALSE = 0, TRUE } match_flag; void main() { int index = 0; int count_of_letter = 0; int count_of_space = 0; char str[] = "I'm Ely efod"; match_flag = FALSE; for(; str[index] != '\0'; index++) if( SPACE != str[index] ) count_of_letter++; else { match_flag = (enum BOOLEAN) 1; count_of_space++; } printf("%s %d times %c", match_flag ? "match" : "not match", count_of_space, NEWLINE); printf("count of letters: %d %c%c", count_of_letter, NEWLINE, RETURN); } : match 2 times count of letters: 10 Press any key to continue 4. sizeof #include <stdio.h> enum escapes { BELL = '\a', BACKSPACE = '\b', HTAB = '\t', RETURN = '\r', NEWLINE = '
    ', VTAB = '\v', SPACE = ' ' }; enum BOOLEAN { FALSE = 0, TRUE } match_flag; void main() { printf("%d bytes
    ", sizeof(enum escapes)); //4 bytes printf("%d bytes
    ", sizeof(escapes)); //4 bytes printf("%d bytes
    ", sizeof(enum BOOLEAN)); //4 bytes printf("%d bytes
    ", sizeof(BOOLEAN)); //4 bytes printf("%d bytes
    ", sizeof(match_flag)); //4 bytes printf("%d bytes
    ", sizeof(SPACE)); //4 bytes printf("%d bytes
    ", sizeof(NEWLINE)); //4 bytes printf("%d bytes
    ", sizeof(FALSE)); //4 bytes printf("%d bytes
    ", sizeof(0)); //4 bytes } 5. #include<stdio.h> enum Season { spring, summer=100, fall=96, winter }; typedef enum { Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday } Weekday; void main() { /* Season */ printf("%d
    ", spring); // 0 printf("%d, %c
    ", summer, summer); // 100, d printf("%d
    ", fall+winter); // 193 Season mySeason=winter; if(winter==mySeason) printf("mySeason is winter
    "); // mySeason is winter int x=100; if(x==summer) printf("x is equal to summer
    "); // x is equal to summer printf("%d bytes
    ", sizeof(spring)); // 4 bytes /* Weekday */ printf("sizeof Weekday is: %d
    ", sizeof(Weekday)); //sizeof Weekday is: 4 Weekday today = Saturday; Weekday tomorrow; if(today == Monday) tomorrow = Tuesday; else tomorrow = (Weekday) (today + 1); //remember to convert from int to Weekday }

     
     
    C     -     
    
     :            VC++ 6.0
    
    
    
        ,               ,           #define       ,       :
    
    
    
    
    
    #define MON  1
    
    #define TUE   2
    
    #define WED  3
    
    #define THU   4
    
    #define FRI    5
    
    #define SAT   6
    
    #define SUN   7
    
    
    
     
    
    
    
      ,            ,           。            。
    
    
    
     
    
    
    
    1.            -    
    
    
    
                     -    
    
    
    
    enum DAY
    
    {
    
          MON=1, TUE, WED, THU, FRI, SAT, SUN
    
    };
    
     
    
    
    
    (1)         ,      (    )          ,       ,  。
    
    
    
    (2) DAY      ,           ,      ,        。
    
    
    
    (3)                0,                1。
    
    
    
    (4)             ,             。
    
    
    
    (5)          #define   。
    
    
    
    (6)        ;  。
    
    
    
     
    
    
    
    2.              
    
    
    
               ,       。              , :  int,       float,       double,    char,    short  。                  :
    
    
    
    
    
    char     a; //  a        char
    
    char     letter;
    
    int        x,
    
               y,
    
               z; //  x,y z       int
    
    int       number;
    
    double  m, n;
    
    double  result; //  result          double
    
    
    
     
    
    
    
                ,                      。
    
    
    
       :               
    
    
    
    enum DAY
    
    {
    
          MON=1, TUE, WED, THU, FRI, SAT, SUN
    
    };
    
     
    
    
    
    enum DAY yesterday;
    
    enum DAY today;
    
    enum DAY tomorrow; //  tomorrow       enum DAY
    
    enum DAY good_day, bad_day; //  good_day bad_day        enum DAY
    
     
    
    
    
       :             :
    
    
    
    
    
    enum //          ,     DAY  ,     。
    
    {
    
        saturday,
    
        sunday = 0,
    
        monday,
    
        tuesday,
    
        wednesday,
    
        thursday,
    
        friday
    
    } workday; //  workday       enum DAY
    
    
    
     
    
    
    
    enum week { Mon=1, Tue, Wed, Thu, Fri Sat, Sun} days; //  days       enum week
    
     
    
    
    
    enum BOOLEAN { false, true } end_flag, match_flag; //                 
    
     
    
    
    
       : typedef             ,            :
    
    
    
    
    
    typedef enum workday
    
    {
    
        saturday,
    
        sunday = 0,
    
        monday,
    
        tuesday,
    
        wednesday,
    
        thursday,
    
        friday
    
    } workday; //   workday    enum workday   
    
    
    
     
    
    
    
    workday today, tomorrow; //  today tomorrow       workday,  enum workday
    
     
    
    
    
    enum workday  workday    :
    
    
    
    
    
    typedef enum
    
    {
    
        saturday,
    
        sunday = 0,
    
        monday,
    
        tuesday,
    
        wednesday,
    
        thursday,
    
        friday
    
    } workday; //   workday    enum workday   
    
    
    
    workday today, tomorrow; //  today tomorrow       workday,  enum workday
    
    
    
     
    
    
    
            :
    
    
    
    
    
    typedef enum workday
    
    {
    
        saturday,
    
        sunday = 0,
    
        monday,
    
        tuesday,
    
        wednesday,
    
        thursday,
    
        friday
    
    };
    
    
    
    workday today, tomorrow; //  today tomorrow       workday,  enum workday
    
    
    
     
    
    
    
      :                 ,                    。        :
    
    
    
         :         
    
    
    
    
    
    typedef enum
    
    {
    
        wednesday,
    
        thursday,
    
        friday
    
    } workday;
    
    
    
    typedef enum WEEK
    
    {
    
        saturday,
    
        sunday = 0,
    
        monday,
    
    } workday;
    
    
    
     
    
    
    
         :         
    
    
    
    
    
    typedef enum
    
    {
    
        wednesday,
    
        thursday,
    
        friday
    
    } workday_1;
    
    
    
    typedef enum WEEK
    
    {
    
        wednesday,
    
        sunday = 0,
    
        monday,
    
    } workday_2;
    
    
    
     
    
    
    
    
    
    3.          
    
    
    
    3.1          。
    
    
    
                             :
    
    
    
       :     ,      
    
    
    
    
    
    #include<stdio.h>
    
    
    
    /*        */
    
    enum DAY { MON=1, TUE, WED, THU, FRI, SAT, SUN };
    
    
    
    void main()
    
    {
    
        /**/
    
        int x, y, z;
    
        
    
        x = 10;
    
        y = 20;
    
        z = 30;
    
        
    
        /**/
    
        enum DAY yesterday, today, tomorrow;
    
        
    
        yesterday = MON;
    
        today     = TUE;
    
        tomorrow  = WED;
    
    
    
        printf("%d %d %d 
    ", yesterday, today, tomorrow); } : #include <stdio.h> /* */ enum DAY { MON=1, TUE, WED, THU, FRI, SAT, SUN }; void main() { /* */ int x=10, y=20, z=30; /* */ enum DAY yesterday = MON, today = TUE, tomorrow = WED; printf("%d %d %d
    ", yesterday, today, tomorrow); } : , 。 #include <stdio.h> /* , , */ enum DAY { MON=1, TUE, WED, THU, FRI, SAT, SUN } yesterday, today, tomorrow; /**/ int x, y, z; void main() { /* */ x = 10; y = 20; z = 30; /* */ yesterday = MON; today = TUE; tomorrow = WED; printf("%d %d %d
    ", x, y, z); // :10 20 30 printf("%d %d %d
    ", yesterday, today, tomorrow); // :1 2 3 } : , , 。 #include <stdio.h> /* , , 。 */ enum DAY { MON=1, TUE, WED, THU, FRI, SAT, SUN } yesterday = MON, today = TUE, tomorrow = WED; /* , 。 */ int x = 10, y = 20, z = 30; void main() { printf("%d %d %d
    ", x, y, z); // :10 20 30 printf("%d %d %d
    ", yesterday, today, tomorrow); // :1 2 3 } 3.2 , 。 #include <stdio.h> enum DAY { MON=1, TUE, WED, THU, FRI, SAT, SUN }; void main() { enum DAY yesterday, today, tomorrow; yesterday = TUE; today = (enum DAY) (yesterday + 1); // tomorrow = (enum DAY) 30; // //tomorrow = 3; // printf("%d %d %d
    ", yesterday, today, tomorrow); // :2 3 30 } 3.3 #include<stdio.h> enum { BELL = '\a', BACKSPACE = '\b', HTAB = '\t', RETURN = '\r', NEWLINE = '
    ', VTAB = '\v', SPACE = ' ' }; enum BOOLEAN { FALSE = 0, TRUE } match_flag; void main() { int index = 0; int count_of_letter = 0; int count_of_space = 0; char str[] = "I'm Ely efod"; match_flag = FALSE; for(; str[index] != '\0'; index++) if( SPACE != str[index] ) count_of_letter++; else { match_flag = (enum BOOLEAN) 1; count_of_space++; } printf("%s %d times %c", match_flag ? "match" : "not match", count_of_space, NEWLINE); printf("count of letters: %d %c%c", count_of_letter, NEWLINE, RETURN); } : match 2 times count of letters: 10 Press any key to continue 4. sizeof #include <stdio.h> enum escapes { BELL = '\a', BACKSPACE = '\b', HTAB = '\t', RETURN = '\r', NEWLINE = '
    ', VTAB = '\v', SPACE = ' ' }; enum BOOLEAN { FALSE = 0, TRUE } match_flag; void main() { printf("%d bytes
    ", sizeof(enum escapes)); //4 bytes printf("%d bytes
    ", sizeof(escapes)); //4 bytes printf("%d bytes
    ", sizeof(enum BOOLEAN)); //4 bytes printf("%d bytes
    ", sizeof(BOOLEAN)); //4 bytes printf("%d bytes
    ", sizeof(match_flag)); //4 bytes printf("%d bytes
    ", sizeof(SPACE)); //4 bytes printf("%d bytes
    ", sizeof(NEWLINE)); //4 bytes printf("%d bytes
    ", sizeof(FALSE)); //4 bytes printf("%d bytes
    ", sizeof(0)); //4 bytes } 5. #include<stdio.h> enum Season { spring, summer=100, fall=96, winter }; typedef enum { Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday } Weekday; void main() { /* Season */ printf("%d
    ", spring); // 0 printf("%d, %c
    ", summer, summer); // 100, d printf("%d
    ", fall+winter); // 193 Season mySeason=winter; if(winter==mySeason) printf("mySeason is winter
    "); // mySeason is winter int x=100; if(x==summer) printf("x is equal to summer
    "); // x is equal to summer printf("%d bytes
    ", sizeof(spring)); // 4 bytes /* Weekday */ printf("sizeof Weekday is: %d
    ", sizeof(Weekday)); //sizeof Weekday is: 4 Weekday today = Saturday; Weekday tomorrow; if(today == Monday) tomorrow = Tuesday; else tomorrow = (Weekday) (today + 1); //remember to convert from int to Weekday }