# Passing arguments to thread in C



## Business_Woman (Jul 28, 2011)

Hi,

Im trying to figure out how to successfully pass a struct as an argument to a thread.
But i can't seem to get it right. Ideas?


```
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <string.h>
    
/* Prototypes */
void *run(void *thread_arg);



struct thread_args {
       
      int arraylength;
      int *ptr;
      char *name;
};
        
int main() {
        
  pthread_t thread_id;
  struct thread_args  pthread_data;
  struct thread_args *thread_data = &pthread_data;
   
    
  thread_data->arraylength = 10;
 
  strcpy(thread_data->name,"David");
  
  pthread_create(&thread_id,NULL,run,(void *)&thread_data);
  pthread_join(thread_id,NULL);
  pthread_exit(NULL);
  return 0;
  }    
       

       
 void *run(void *thread_arg) {
        
        struct thread_args *my_data;
        my_data = (struct thread_args *)thread_arg;
        printf("%s",my_data->name);   //Supposed to print "David"
         
}
```


----------



## fonz (Jul 28, 2011)

Business_Woman said:
			
		

> Hi,
> 
> Im trying to figure out how to successfully pass a struct as an argument to a thread.
> But i can't seem to get it right.


Actually, you do - more or less. The error is somewhere else. Read on.


```
struct thread_args  pthread_data;
struct thread_args *thread_data = &pthread_data;
```
Although not incorrect, I find this ugly. Why declare a seperate pointer _thread_data_ when you can simply use _&pthread_data_? See the next point...

```
pthread_create(&thread_id,NULL,run,(void *)[red]&[/red]thread_data);
```
Ditch the ampersand (or just write _&pthread_data_ in the first place), _thread_data_ is already a pointer and you're passing a pointer to that pointer but in _run()_ you cast it back to the pointer itself.


```
strcpy(thread_data->name,"David");
```
But this is your actual mistake. Can you tell why? (hint: think memory allocation)

Hope this helps,

Fonz


----------



## Business_Woman (Jul 28, 2011)

fonz said:
			
		

> Actually, you do - more or less. The error is somewhere else. Read on.
> 
> 
> ```
> ...



Hm, you have to malloc some memory for the pointer to point to?


----------



## fonz (Jul 28, 2011)

Business_Woman said:
			
		

> Hm, you have to malloc some memory for the pointer to point to?


Yep. Either explicitly allocate memory for _pthread_data.name_ (the destination string) by using malloc(3) or calloc(3) manually, or do it implicitly by instead of strcpy(3) using another function that does the allocation for you, such as strdup(3) or asprintf(3).

Hope this helps,

Fonz


----------



## expl (Jul 28, 2011)

Simply changing to:


```
thread_data->name = "David";
```

Will do the trick. (In current context)


Edit:

By the way, you should call pthread_exit() in your children threads and not in main().


----------



## fonz (Jul 28, 2011)

Plus the change in the call to pthread_create(3), that is. Use either _thread_data_ or _&pthread_data_, not _&thread_data_ since that will still segfault as explained above.

Fonz


----------



## Business_Woman (Jul 29, 2011)

Thank you Fonzie and expl!

It seems to be working now


----------



## fonz (Jul 29, 2011)

Business_Woman said:
			
		

> Thank you Fonzie and expl!


Eeey, you're welcomundo 

Fonz


----------

