您的位置:首页 > 其它

结构体二级指针的初始化

2016-04-15 17:12 211 查看
#include <stdio.h>

#include <string.h>

#include <stdlib.h>

#include <unistd.h>

struct infomation{

char plate_num[16];

char time_str[32];

};

int main(void)

{

int i = 0;

struct infomation **p2Info = malloc( sizeof(struct infomation * ));

if(p2Info == NULL){

printf(" %d cannot malloc mem!\n", __LINE__);

return -1;

}

for(i = 0; i < 20; i++){

*(p2Info + i) = malloc(sizeof(struct infomation ));

if(*(p2Info + i) == NULL){

printf(" %d cannot malloc mem!\n", __LINE__);

return -1;

}

strcpy((*(p2Info + i))->plate_num, "hello world");

strcpy((*(p2Info + i))->time_str, "2015-03-21");

printf("%s %s\n", (*(p2Info + i))->plate_num, (*(p2Info + i))->time_str);

}

return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: