In the following code I am calling a function from a pthread:

pthread_create(&threads[t], NULL, dnssearch, (void *)buffer)

I need to pass the string (buffer) to a function and the have the function pass a string back. What am I doing wrong in the function:

void *dnssearch (void *buf)
{
 char *abc = (void *)buf;
 char tur[30],rut[30];
 int strncmp(const char *abc, const char *tur, size_t n);
 FILE *f;
 int c=0,i;

 printf("%s\n",abc);
 f = fopen("dnsfile", "r");
 for (i=0; i<30 && abc[i] != 0xA; i++);
 abc[i]='\0';
 if ( f != NULL)
 {
    do
       {
          fscanf(f, "%s", tur);
          if (strncmp(tur,abc,30) == 0)
             {
                fscanf(f, "%s\n", tur);
                printf("%s\n",tur);
                strcpy(abc,tur);
                printf("%s\n",abc);
             }
          c=c++;
       }
       while (c <=10 );
}
 fclose(f);
 strcpy(buf,abc);
 return abc;
}

You can't make the return value of that function anything other than an integer. Why? Because they are not normal functions -- it acts more like a self-contained process than another function, except that threads have access to all global data in the entire program. So when a thread returns it just goes back to the operating system much like any other program would do.

The code you already put on line 30 should be sufficient. The return value on line 31 is meaningless.

You can't make the return value of that function anything other than an integer. Why? Because they are not normal functions -- it acts more like a self-contained process than another function, except that threads have access to all global data in the entire program. So when a thread returns it just goes back to the operating system much like any other program would do.

Wrong. The thread process is prototyped to return void * with a purpose. You may return anything it wants. The calling thread may collect the returned value via pthread_join() .

In the following code I am calling a function from a pthread:

pthread_create(&threads[t], NULL, dnssearch, (void *)buffer)

I need to pass the string (buffer) to a function and the have the function pass a string back. What am I doing wrong in the function:

Quite a lot. Starting from a possible buffer overflow in fscanf, and invoking undefined behaviour at line 25, to begin with. Dangerously aliasing an argument. Probably not joining the thread.
Also, it is very unclear what the function is supposed to do; its expected result and actual behaviour. Can you explain it in plain English (especially the do loop at lines 15 to 27)?

commented: Glad to know that. Not the first time I've been wrong about something :) +28

The do loop is reading a file searching for a specific string that is passed through. Once it sees the specific string it reads the next string and that string is then passed back when the function ends. I know when I created and tested the function that I was able to pass the desired string back through the function. I am required to run the function through a thread. When running the function through a pthread is when I am not getting the desired information back. It may be there, I just don't know how to retrieve the string. I take it that pthread_join will help. I have never dealt with pthreads at all. I do appreciate all the helping information..

OK.. I now have string returning from function but when I get the string is has weird characters in the first 4 positions. Why is it getting the weird characters? Here is my pthread:

pthread_t threads[NUM_THREADS];
      int rc, t;
      for(t=0; t<NUM_THREADS; t++)
      {
         printf("In main: creating thread %d\n", t);
         rc = pthread_create(&threads[t], NULL, dnssearch, (void *)buffer);
          if (rc)
          {
            printf("ERROR; return code from pthread_create() is %d\n", rc);
            exit(-1);
          }
       }
     pthread_join(threads[0],(void *)buffer);

Nevermind on this. I have been going about my C program the wrong way. I am rewriting to do a dns query using gethostbyname... Sorry for the trouble.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.