5

Hallo All,

I have this method:

void *readFileLocal(char filename[]){
       .....
}

Now i want to start this method a a thread:

char input[strlen(argv[1])];
strcpy(input,argv[1]);
pthread_t read,write;
pthread_create(&read, NULL, &readFileLocal, &input);

But during compilation it gives this warning:

file.c:29: warning: passing argument 3 of ‘pthread_create’ from incompatible pointer type

/usr/include/pthread.h:227: note: expected ‘void * (*)(void *)’ but argument is of type ‘void * (*)(char *)’

How can I parse an char array to my funcation over pthread_create without this warning ? Thanks for helpt

0

3 Answers 3

3

Just use this:

pthread_create(&read, NULL, readFileLocal, input);

And consider changing your function's signature to:

void *readFileLocal(void *fileName) { }

When you are passing a pointer to function (like the one you are using in readFileLocal parameter) you don't need to put &.

Also, when you have an array (like input in your case) you don't need & since in C arrays can be used as pointers already.

Sign up to request clarification or add additional context in comments.

3 Comments

C arrays are not pointers, they just tend to decay into pointers at the slightest provocation.
@Hasturkun: Agree. I don't think it is relevant to the question though.
It isn't really, except for furthering the strange myth claiming that arrays are pointers
2

Functions for threads need to be prototyped:

void *func(void *argv);

As with all void pointers you then need to interpret ("cast") the pointer to a meaningful entity. You readFileLocal functions then becomes:

void *readFileLocal(void *argv)
{
    char *fname = argv; // Cast to string
    // Rest of func
}

1 Comment

+1 for "casting" without the cast operator. The operation is called "convert": there are implicit conversions (done by the compiler without the cast) and explicit conversions (done with the cast operator).
0

It just needs to look like this.

Don't forget to force conversion of types.

void *task(void *arg) {
    // printf("My value is %s", (char *) arg);
    return 0;
}


int main(){
    char* value = "StackOverflow";
    
    pthread_t t;
    pthread_create(&t, NULL, task, value);
}

Comments

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.