- #1
James889
- 192
- 1
Hi,
I am trying to figure out how to pass arguments to a thread via a struct.
It's not really working out for me.
What am i doing wrong here?
I am trying to figure out how to pass arguments to a thread via a struct.
It's not really working out for me.
What am i doing wrong here?
Code:
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <string.h>
void *run(void *thread_arg);
struct thread_args {
int arraylength;
int *ptr;
char *name;
};
int main() {
pthread_t thread_id;
struct thread_args *thread_data;
int array[10] = { 1,2,3,4,5,6,7,8,9,10 };
thread_data->arraylength = 10;
thread_data->ptr = &array[0];
strcpy(thread_data->name,"David"); /* Is this correct ? */
printf("%s",thread_data->name); //This prints nothing
pthread_create(&thread_id,NULL,run,(void *)&thread_data);
pthread_join(thread_id,NULL);
return 0;
}
void *run(void *thread_arg) {
printf("hi");
struct thread_args *my_data;
my_data = (struct thread_args *)thread_arg;
printf("%s",my_data->name);
}