当前位置: 首页 > article >正文

柔性数组的使用

//柔性数组的使用
#include<stdio.h>
#include<stdlib.h>
#include<errno.h>
struct s
{
	int i;
	int a[];
};
int main()
{
	struct s* ps = (struct s*)malloc(sizeof(struct s) + 20 * sizeof(int));
	if (ps == NULL)
	{
		perror("malloc");
		return 1;
	}
	//使用这块空间
	ps->i = 100;
	int j = 0;
	for (j = 0; j < 20; j++)
	{
		ps->a[j] = j + 1;
	}
	//调整ps指向空间的大小
	struct s* str = (struct s*)realloc(ps, sizeof(struct s) + 40 * sizeof(int));
	if (str != NULL)
	{
		ps = str;
		str = NULL;
	}
	//使用空间后面开辟的40个字节,打印出来
	for (j = 0; j < 40; j++)
	{
		ps->a[j] = j + 1;
	}
	for (j = 0; j < 40; j++)
	{

		if (j % 10 == 0)
		{
			printf("\n");
		}
		printf("%d  ", ps->a[j]);
	}
	//释放空间
	free(ps);
	ps = NULL;
	return 0;
}

输出结果:


http://www.kler.cn/news/360830.html

相关文章:

  • 基于ECS和NAS搭建个人网盘
  • Java访问修饰符private,default,protected,public
  • LeetCode_2413. 最小偶倍数_java
  • 基于Multisim的任意进制计数器设计与仿真
  • 【Linux 从基础到进阶】磁盘I/O性能调优
  • uniapp 实现input聚焦时选中内容(已封装)兼容微信小程序
  • uniapp的相关知识(2)
  • 10.22学习
  • Recall/Coarse Ranking/Fine Ranking
  • 每日算法一练:剑指offer——数组篇(3)
  • 图像异常检测评估指标-分类性能
  • 存-20241019 CSPJ模拟测试5 题解
  • 免费开源的微信开发框架
  • CentOS安装Docker教程,及报错。
  • RK3568平台开发系列讲解(调试篇)debugfs 文件系统
  • C++中static关键字的用法(实现日期类)
  • 【Vulnhub靶场】Kioptrix Level 5
  • 【算法】深入了解 CRC 校验码的计算过程
  • 【计网笔记】应用层
  • 深度学习的高级应用