2

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.

In the get_checksum function,

void* get_checksum(void* a){

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

I passed in a as a char pointer in relation to filenames[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.

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

1
  • 2
    Create a structure, populate the structure with all the relevant parameters, and pass that in to pthread_create - amparo.net/ce155/thread-ex.html Commented Mar 26, 2016 at 20:26

2 Answers 2

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.

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);
    /* ... */
}
Sign up to request clarification or add additional context in comments.

3 Comments

Assignment from incompatible pointer type at p->filename = &filenames[i]
Possible filenames is const char filenames[] ? If so than params.filename needs to be const char *.
char **filenames so would that syntax still apply?
2

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

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:

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]);
}

2 Comments

So I suppose you have to decide whether heap or stack allocation works for you. If the threads live past the scope of the function, bad things happen, but they might not in this case. Please note that NUMBER_OF_THREADS is not a constant so global allocation won't work.
@Joshua. You are right. But what I wrote is just an example. In my answer I wrote that the OP should either use it statically or allocate it dynamically. It's OPs choice according to the design.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.