The difference between struct and typedef struct in C and C ++ in detail

c test code

 struct Cmstruct 
    { 
        int C; 
    } Cm is; // Cm is a variable 
    typedef struct MyStruct // MyStruct herein may be omitted 
    {
         int m; 
    } My; // My alias is struct MyStruct 
    My ST;
     struct Cmstruct CM1;
     // only use can not be used to define struct Cmstruct Cmstruct 
    st.m = . 1 ; 
    the printf ( " % D \ n- " , st.m); // . 1
     // struct MyStruct ST1; given, this method can not be reused, only ST1 My 
    Cm is = .c 10  ;
    the printf ("%d\n", Cm.c); // 10
    cm1.c = 22;
    printf("%d\n", cm1.c); // 22

 c ++ test code

struct MyStruct 
    { 
        int m; 
    } cm; // above mentioned cm in variable c 
    cm.m = 20 is ; 
    the printf ( " % D \ n- " , cm.m); // 20 is 
    struct MyStruct my; // declare my You may be omitted struct, c, but can not be omitted 
    my.m = 10 ; 
    the printf ( " % D \ n- " , my.m); // 10 
    typedef struct Mystruct1 
    { 
        int S; 
    } Sname; // Sname alias is struct Mystruct1 
    sname ss; 
    ss.s = 22 is ; 
    the printf ( " % D \ n- " , ss.s); // 22 is 

    struct Mystruct1 SS1; // struct Mystruct1 may be omitted in the struct
     // here with different C, after struct aliases in C struct Mystruct1 statement can not be used 
    ss1.s = 33 is ; 
    the printf ( " % D \ n- " , ss1.s); // 22 is

to sum up:

C and C ++ the same point
no time typedef cm / Cm is a variable declarations, it is time typedef My / Sname struct is an alias.
C and C ++ differences
do not typedef time, C struct Mystruct can only be used to declare variables, C ++ can be omitted struct.
There typedef time, C can only use the alias to declare variables, C ++ can still use the struct Mystruct or struct omitted.

Guess you like

Origin www.cnblogs.com/gavinpan/p/11395199.html