zzy-c

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
  84 随笔 :: 0 文章 :: 0 评论 :: 12117 阅读
复制代码
#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   zzy_C  阅读(102)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示