动态二维数组指针使用示例

 

 

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>

struct infomation{
    char plate_num[16];
    char time_str[32];
};

#define ROWNUM 20
#define COLUMN 5

int main(void)
{
    int i = 0;
    // 20*5
    struct infomation **p2Info = (struct infomation **)malloc(ROWNUM * sizeof(struct infomation * ));
    if(p2Info == NULL){
        printf(" %d cannot malloc mem!\n", __LINE__);
        return -1;
    }
    for(i = 0; i < ROWNUM; i++){
        //*(p2Info + i) = (struct infomation *)malloc(sizeof(struct infomation ));
        p2Info[i] = (struct infomation *)malloc(sizeof(struct infomation ));
        if(*(p2Info + i) == NULL){
            printf(" %d cannot malloc mem!\n", __LINE__);
            return -1;
        }
        memset(*(p2Info + i), 0x00, sizeof(struct infomation ));

        strcpy((*(p2Info + i))->plate_num, "hello world");
        strcpy((*(p2Info + i))->time_str, "2015-03-21");
        printf("%d:%s %s\n", i, (*(p2Info + i))->plate_num, (*(p2Info + i))->time_str);

    }

    for(int i=0; i < ROWNUM; ++i)
        free(p2Info[i]);
    free(p2Info);
    return 0;
}

 

posted @ 2018-06-18 09:38  PKICA  阅读(205)  评论(0编辑  收藏  举报