DunkRuZ
DunkRuZ

Reputation: 83

Dereferecing a double pointer coming into a function and then passing to another

I am having problems doing the following scenario. I have a function which comes as:

void testing(mystruct str**) {
    pthread threads[5];
    for(int i = 0; i < 5; i++) {
        pthread_create(&thread[i], NULL, process, (void)&(*str));   //not really sure how to pass this here
    }
}

void * process(void *arg) {
    struct mystruct **value = arg;   //probably wrong but it compiles, then blows up some where else because its not really the value that was original passed.
}

I am new to C and as far as I understand this is whats its doing,

but i get "dereferencing pointer to incomplete type"

any help understanding this would be much appreciated.

Upvotes: 0

Views: 104

Answers (1)

Lundin
Lundin

Reputation: 213513

You should be able to pass on the pointer as-is, since every object pointer type in C can be implicitly converted to/from void*. Still, it is more correct to de-reference the pointer one level. That is:

pthread_create(&thread[i], NULL, process, *str);

IMPORTANT: This assumes that str points at a variable which will not go out of scope while the thread is executing! Passing on pointers to local variables to threads is a common source of bugs.

From there on, your thread callback should do something like this:

void * process(void *arg) {
    struct mystruct* ms = arg; 
    struct mystruct** ptr_ms = &arg; 

However, the error "dereferencing pointer to incomplete type" simply means that the struct definition isn't visible to the thread, causing the struct type to get treated as incomplete. So the issue might as well be a missing #include.

Upvotes: 2

Related Questions