如何知道int *数组的结尾? [英] How to know the end of int* array?

查看:370
本文介绍了如何知道int *数组的结尾?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用malloc()创建具有int*数据类型的动态数组.但是问题是,如何知道数组的结尾?

I'm making a dynamic array with int* data type using malloc(). But the problems is, how to know end of array?

对于int*数据类型,没有等效于\0的东西,那么,该怎么做呢?通过大小作为函数的out参数?

There no an equivalent to \0 for int* data type,so, how to do this? Pass size as out parameter of function?

推荐答案

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

C doesn't manage array lengths, as some other languages do.

您可能会考虑采用以下结构:

you might consider a structure for this:

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

正在使用:

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

这篇关于如何知道int *数组的结尾?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆