c语言数组长度和指针的使用-这样写报错不报错

 

include                                                                                                                                                               
#include                                                                                                                                                              
#include                                                                                                                                                              
#include                                                                                                                                                              
                                                                                                                                                                                
typedef struct{                                                                                                                                                                 
    int data;                                                                                                                                                                   
}STRU_FOR_ARRAY;                                                                                                                                                                
                                                                                                                                                                                
typedef struct {                                                                                                                                                                
    int flg;                                                                                                                                                                    
    STRU_FOR_ARRAY myarry[1];                                                                                                                                                   
} MyStru;                                                                                                                                                                       
                                                                                                                                                                                
void checkstry(){                                                                                                                                                               
    MyStru * stru = (MyStru *)malloc(sizeof(MyStru) + sizeof(STRU_FOR_ARRAY)*8);                                                                                                
    memset(stru, 0, sizeof(MyStru) + sizeof(STRU_FOR_ARRAY)*8);                                                                                                                 
    stru->flg = 8;                                                                                                                                                              
    for(int i=0; iflg; i++){                                                                                                                                             
        stru->myarry[i].data = i;                                                                                                                                               
    }                                                                                                                                                                           
                                                                                                                                                                                
    for(int j=0;j<8;j++){                                                                                                                                                       
        printf("check array, flg: %d \n", stru->myarry[j].data);                                                                                                                
    }                                                                                                                                                                           
}                                                                                                                                                                               
                                                                                                                                                                                
                                                                                                                                                                                
int main(int argc, char**argv)                                                                                                                                                  
{                                                                                                                                                                               
    checkstry();                                                                                                                                                                
                                                                                                                                                                                
    return 0;                                                                                                                                                                   
}

运行结果:

check array, flg: 0                                                                                                                                                             
check array, flg: 1                                                                                                                                                             
check array, flg: 2                                                                                                                                                             
check array, flg: 3                                                                                                                                                             
check array, flg: 4                                                                                                                                                             
check array, flg: 5                                                                                                                                                             
check array, flg: 6                                                                                                                                                             
check array, flg: 7

就是这么神奇!

你可能感兴趣的:(programming)