zzy-c

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include<string.h>
#include<malloc.h>
struct S
{
    int n;
    int arr[];//大小未知 //这个数组就是柔性数组
};
int main()
{
    //期望arr的大小是10个整形
    struct S *ps = (struct S*)malloc(sizeof(struct S)+10*sizeof(int));
    ps->n = 10;
    int i = 0;
    for (i = 0; i < 10; i++)
    {
        ps->arr[i] = i;
    }
    //增加
    struct S* ptr = (struct S*)realloc(ps, sizeof(struct S) + 20 * sizeof(int));
    if (ptr != NULL)
    {
        ps = ptr;
    }
    //使用

    //释放
    free(ps);
    ps = NULL;
    /*struct S s = { 0 };
    printf("%d\n", sizeof(s));*/
    return 0;
}

 

posted on 2022-04-15 18:11  zzy_C  阅读(107)  评论(0)    收藏  举报