Remember that &argv[1]
is a char*[]
, i.e. an array of pointers to characters. Casting it to a char *
i.e. pointer to characters is incorrect.
Try:
pthread_create(&thread1, NULL, getMax, argv[1]);
سؤال
I am trying to create a thread that takes the character string form argv[1]
and passes it to my function. This is the code I am trying to use to pass it.
if(pthread_create(&thread1, NULL, getMax, &argv[1]) != 0){
printf("ERROR createing the thread\n");
return 1;
}
This is my function that i am calling in the pthread_create function.
void * getMax(void * f){
char * fileName = (char*)f;
printf("%s\n\n",fileName);
}
I believe my problem is when I am casting it back to a character pointer. The printf
function prints out a couple of random characters. If I call the function passing in a string it works.
pthread_create(&thread1, NULL, getMax, "This Works");
If someone could explain how to cast the argv[1]
so that it behaves like a character array that would be greatly appreciated.
المحلول
Remember that &argv[1]
is a char*[]
, i.e. an array of pointers to characters. Casting it to a char *
i.e. pointer to characters is incorrect.
Try:
pthread_create(&thread1, NULL, getMax, argv[1]);