为什么我不能将数组值返回给主函数?

why can't i return array values to main function?

本文关键字:函数 返回 不能 数组 为什么      更新时间:2023-10-16

我在 Linux 中创建了一个用于创建线程的主函数,其中包含读取该数据的函数。 我的主代码包含线程初始化为:

主要功能:

int main(int argc , /*no of aruments*/
            char *argv[])/*store each argument values*/
 {
    pthread_t thid[count];
    create_thread(argv,count,&thid);
 }

和我的函数为:

int create_thread(char *argv[],int count , pthread_t **thid)
{
     for(index = 1; index <= count; index++)
    {
         status = pthread_create(&thid[index],NULL,file_op,(void*)   mystruct);/*create main threads*/
    }
}

我收到错误,例如

function.c:: warning: passing argument 1 of ‘pthread_create’ from incompatible pointer type
 /usr/include/pthread.h: note: expected ‘pthread_t * __restrict__’ but argument is of type ‘pthread_t **

main.c: In function ‘main’:
main.c:: warning: passing argument 3 of ‘create_thread’ from    incompatible pointer type
function.c:: note: expected ‘pthread_t **’ but argument is of type ‘pthread_t (*)[(long unsigned int)(count)]’

线程代码有问题吗? 我如何声明正确的语法?我想获取从函数到主数组的所有值。

进行两项更改:

create_thread(argv,count,thid);

int create_thread(char *argv[],int count , pthread_t *thid)

这会将数组传递给您的函数,并将传递一个指针,指向要由 pthread_create 更新的线程 ID 之一。

pthread_create函数的第一个参数需要pthread_t *。您传递了错误类型的参数。

看看这个: pthread_create

此外,create_thread() 正在写入 thid 数组的末尾。循环应该是

for (index = 0; index < count; index++)