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?
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?
Code:
#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"
}