这是在C中创建数组的有效方法吗?(Is this a valid method of creating an array in C?)

编程入门 行业动态 更新时间:2024-10-28 06:22:33
这是在C中创建数组的有效方法吗?(Is this a valid method of creating an array in C?) int threads = 5; pthread_t * thread = malloc(sizeof(pthread_t)*threads); for (i = 0; i < threads; i++){ int ret = pthread_create(&thread[i], NULL, &foobar_function, NULL);}

我现在无法运行代码。 但我看到这是一个在线示例的一部分,并且由于缺少方括号而有点困惑。 我对C不太好

这对于创建线程数组有用吗?

int threads = 5; pthread_t * thread = malloc(sizeof(pthread_t)*threads); for (i = 0; i < threads; i++){ int ret = pthread_create(&thread[i], NULL, &foobar_function, NULL);}

I'm not in a position to run the code right now. But I saw this as part of an online example and was a little confused by the total lack of square brackets. I'm not great with C.

So does this work for creating an array of threads?

最满意答案

是。

thread指向malloc分配的malloc块,该内存块足够大以容纳threads pthread_t对象。

pthread_t对象的threads数组可以用这种方式表示。

Yes.

thread is pointing at a block of memory allocated by malloc that is large enough to hold threads pthread_t objects.

An array of threads pthread_t objects can be represented in exactly this way.

更多推荐

本文发布于:2023-08-01 01:48:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1350492.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:这是   数组   方法   valid   creating

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!