1
votes

can someone tell me what am I doing wrong? (I omitted the rest of the program because its very long...)

#include <pthread.h>

void *RTPfun(char *client_addr);

int main(int argc, char *argv[])
{ 
  char* client_addr;
  pthread_t RTPthread;

  // ...

  pthread_create(&RTPthread, NULL, &RTPfun, client_addr) 
}

void *RTPfun(char * client_addr)
{
  // ...
  return;
}

the error:

TCPserver.c: In function ‘main’:
TCPserver.c:74:5: warning: passing argument 3 of ‘pthread_create’ from incompatible pointer type [enabled by default]
/usr/include/pthread.h:225:12: note: expected ‘void * (*)(void *)’ but argument is of type ‘void * (*)(char *)’
2
Your compiler should also warn you about using return; without an expression in a function with non-void return type. If that return statement is ever actually reached, your program has undefined behavior.Casey

2 Answers

2
votes

Pthread works with functions that receive void* and return void*.

You need to change the parameter of your function from char* to void*. Here's an alternative:

#include <pthread.h>



void *RTPfun(void *client_addr);


int main(int argc, char *argv[])
{ 
  char* client_addr;
  pthread_t RTPthread;

   ...
   ...

  pthread_create(&RTPthread, NULL, &RTPfun, client_addr) 
}



void *RTPfun(void* data)
{
 char *client_addr = (char*)data;
 ....
 return;
}
1
votes

You have to convert your char pointer to a void one.

#include <pthread.h>

void *RTPfun(void *client_addr);

int main(int argc, char *argv[])
{ 
  char* client_addr;
  pthread_t RTPthread;

   ...
   ...

  pthread_create(&RTPthread, NULL, &RTPfun, (void*)client_addr) 
}

void *RTPfun(void * client_addr)
{
 char *something = (char*)client_addr;
 ....
 return;
}

Void pointers are used every time you need to pass some data and you cannot know in advance the type of variable (char*, integer*...) it will be. The function you give to pthread_create takes a void* as input, so you can cast your char pointer to a void one, and do the opposite in RTPfun.