brian nyaberi
brian nyaberi

Reputation: 33

Error with Parameters used in pthread_create

I'm working on the following pthread program that finds the number of substrings in string2 that are in string:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#define NUM_THREADS 4
#define MAX 1024

int n1,n2,i;
char *s1,*s2;
FILE *fp;

char *substring(char *string, int position, int length);
void occurrence();


int readf(FILE *fp)
{
    if((fp=fopen("strings.txt", "r"))==NULL){
        printf("ERROR: can't open strings.txt!\n");
        return 0;
    }
    s1=(char *)malloc(sizeof(char)*MAX);
    if(s1==NULL){
        printf("ERROR: Out of memory!\n");
        return -1;
    }
    s2=(char *)malloc(sizeof(char)*MAX);
    if(s1==NULL){
        printf("ERROR: Out of memory\n");
        return -1;
    }
    /*read s1 s2 from the file*/
    s1=fgets(s1, MAX, fp);
    s2=fgets(s2, MAX, fp);
    n1=strlen(s1);  /*length of s1*/
    n2=strlen(s2)-1; /*length of s2*/
    if(s1==NULL || s2==NULL || n1<n2)  /*when error exit*/
        return -1;
}

int main(void)
{
    pthread_t tid;
    pthread_create(&tid, NULL, occurrence, NULL);
    pthread_join(tid, NULL);
    exit(0);

}

char *substring(char *string, int position, int length)
{
    char *pointer;
    int c;

    pointer = malloc(length+1);

    if (pointer == NULL)
    {
        printf("Unable to allocate memory.\n");
        exit(1);
    }

    for (c = 0 ; c < length ; c++)
    {
        *(pointer+c) = *(string+position-1);
        string++;
    }

    *(pointer+c) = '\0';

    return pointer;
}

void occurrence()
{
    char *string1;
    char *string2;
    char *new_str;
    int counter=0;
    readf(fp);

    string1 = s1;
    string2 = s2;
    new_str = malloc(200);
    for(i=1;i<=strlen(string1);i++)
    {
        new_str = substring(string1,i,strlen(string2));
        if(strcmp(new_str, string2)==0)
        {
            counter++;
        }
    }
    printf("The number of substrings is: %d",counter);
}

When I compile and run it, it runs okay but I get the following warning:

|44|warning: passing argument 3 of 'pthread_create' from incompatible pointer type|
|314|note: expected 'void * (*)(void *)' but argument is of type 'void (*)()'|

I know it has to do with the parameters used for Pthread, however i'm having trouble figuring out how to fix it. Any suggestions?

Upvotes: 0

Views: 36

Answers (2)

Some programmer dude
Some programmer dude

Reputation: 409482

The thread function must follow a specific signature. Even if you don't use the argument and don't return a value, you must still have the correct signature.

If you don't need the argument, just ignore it.

If you don't have a specific return value, just return NULL.

Upvotes: 2

arrowd
arrowd

Reputation: 34421

Just make occurence function accept one void* parameter and the warning go away.

Upvotes: 0

Related Questions