如何将数组索引传递给c中的另一个函数?

时间:2022-09-28 22:56:44

I am trying to figure out how to pass an array index into another function.

我想知道如何将数组索引传递给另一个函数。

Here I have:

这里有:

 for(i=0; i<file_cnt; i++){

          iret1 = pthread_create(&(file[i]), NULL, get_checksum, (void*)&filenames[i]);
          printf("%s\n", filenames[i]);
        }

In the function get_checksum, I eventually need the index of filenames, but i is not available in that function.

在get_checksum函数中,我最终需要文件名的索引,但在该函数中我不可用。

In the get_checksum function,

在get_checksum函数中,

void* get_checksum(void* a){

            char *filename = (char *) a;
.....
}

I passed in a as a char pointer in relation to filenames[i].

我在文件名[I]中传递了一个字符指针。

However, I need to make computations later in the function: get_checksumthat involve getting the index of an array of sums. Therefore, I really need the index of filenames to be passed into the function get_checksum instead.

但是,稍后我需要在函数get_checksum中进行计算,其中涉及到获取一个和数组的索引。因此,我确实需要将文件名的索引传递到函数get_checksum中。

Any suggestions how to pass an array index into another function?

任何建议如何将数组索引传递到另一个函数中?

2 个解决方案

#1


2  

Normally you'd just pass 2 parameters but you can't here because pthread_create so you pass a pointer to a heap-allocated struct.

通常你只需要传递2个参数,但是这里不行,因为pthread_create,所以你要传递一个指针到一个heap分配的结构体。

struct params {
    char *filename;
    int i;
};

/* ... */

for(i=0; i<file_cnt; i++){
      struct params *p = malloc(sizeof(struct params));
      p->filename = filenames[i];
      p->i = i;
      iret1 = pthread_create(&(file[i]), NULL, get_checksum, (void*)p);
      if (iret1) {
          free(p);
          printf("OOPS %s\n", filenames[i]);
      } else {
          printf("%s\n", filenames[i]);
      }
    }

/* ... */

void* get_checksum(void* p){
    char *filename = ((struct params *)p)->filename;
    int i = ((struct params *)p)->i;
    free(p);
    /* ... */
}

#2


2  

You can define a struct to pass all the parameters you need:

您可以定义一个struct来传递所需的所有参数:

typedef struct params_s
{
    char * filename;
    int index;
    // additional params
} params_t;

Then either create static or allocate dinamically a variable of type params_t, initialize it and pass its address to the get_checksum:

然后,创建静态或分配dinamically变量的类型params_t,初始化它并将其地址传递给get_checksum:

EXAMPLE

例子

params_t params[NUM_OF_THREADS];
...
for(i=0; i<file_cnt; i++){
    params[i].filename = &filenames[i];
    params[i].index = i;
    iret1 = pthread_create(&(file[i]), NULL, get_checksum, (void*)&params[i]);
    printf("%s\n", filenames[i]);
}

#1


2  

Normally you'd just pass 2 parameters but you can't here because pthread_create so you pass a pointer to a heap-allocated struct.

通常你只需要传递2个参数,但是这里不行,因为pthread_create,所以你要传递一个指针到一个heap分配的结构体。

struct params {
    char *filename;
    int i;
};

/* ... */

for(i=0; i<file_cnt; i++){
      struct params *p = malloc(sizeof(struct params));
      p->filename = filenames[i];
      p->i = i;
      iret1 = pthread_create(&(file[i]), NULL, get_checksum, (void*)p);
      if (iret1) {
          free(p);
          printf("OOPS %s\n", filenames[i]);
      } else {
          printf("%s\n", filenames[i]);
      }
    }

/* ... */

void* get_checksum(void* p){
    char *filename = ((struct params *)p)->filename;
    int i = ((struct params *)p)->i;
    free(p);
    /* ... */
}

#2


2  

You can define a struct to pass all the parameters you need:

您可以定义一个struct来传递所需的所有参数:

typedef struct params_s
{
    char * filename;
    int index;
    // additional params
} params_t;

Then either create static or allocate dinamically a variable of type params_t, initialize it and pass its address to the get_checksum:

然后,创建静态或分配dinamically变量的类型params_t,初始化它并将其地址传递给get_checksum:

EXAMPLE

例子

params_t params[NUM_OF_THREADS];
...
for(i=0; i<file_cnt; i++){
    params[i].filename = &filenames[i];
    params[i].index = i;
    iret1 = pthread_create(&(file[i]), NULL, get_checksum, (void*)&params[i]);
    printf("%s\n", filenames[i]);
}