gpt4 book ai didi

c - 如何知道 int* 数组的结尾?

转载 作者:太空狗 更新时间:2023-10-29 16:58:19 26 4
gpt4 key购买 nike

我正在使用 malloc() 创建一个数据类型为 int* 的动态数组。但问题是,如何知道数组结束?

对于 int* 数据类型,没有等价于 \0 的东西,那么,怎么办呢?将大小作为函数的输出参数传递?

最佳答案

C 不像其他一些语言那样管理数组长度。

你可能会考虑这样的结构:

typedef struct t_thing {
int* things;
size_t count;
} t_thing;

正在使用中:

t_thing t = { (int*)malloc(sizeof(int) * n), n };

关于c - 如何知道 int* 数组的结尾?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10220124/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com