Reputation: 17
I have to write a program which receives words as arguments.For every argument I have to create a thread that verify if the word is palindrom and in that case it will increment a global variable sum. This is what I did
#include <stdio.h>
#include <string.h>
#include <pthread.h>
#define MAX 15
pthread_mutex_t mtx;
int sum=0;
void *Pal(void *arg) {
char *p=char arg;
// char p=*(int*)arg;
int len,j;
int flag=0;
printf("%s received. ", p);
len= strlen(p);
for (j=0; j<len; j++) {
pthread_mutex_lock(&mtx);
if(p[j] ==p[len-j-1])
flag +=1;
pthread_mutex_unlock(&mtx);
}
if (flag==len) {
printf("%s is palindrome.good job \n", p);
sum +=1;
}
else {
printf("%s is not palindrome.Fail \n", p);
}
}
int main( int argc, char* argv[]) {
int i;
pthread_mutex_init(&mtx, NULL);
pthread_t t[MAX];
for(i=1 ; i<argc; i++)
pthread_create(&t[i], NULL, Pal, argv[i]);
for(i=1 ; i<argc; i++)
pthread_join(t[i], NULL);
printf("The global sum is:%d \n", sum);
return 0;
}
The problem is there: char p=char arg.I don't know how to make the relation between the strings and the arguments. If someone can help me I would be apreciated.
Upvotes: 1
Views: 143
Reputation: 53016
You don't need the cast because void *
is converted to any poitner type without a cast in c, so
char *p = arg;
would work.
I didn't check the rest of the program, so I can't say if the program will work as you expect, but at least this fixes one problem.
Upvotes: 2