I have this project where I recieve input from the command line such as "54 342 12" and it is suppose to make a thread for each input and have the threads return an array of integers and then the main thread is supppose to print out the different prime factorizations. however I am recieving weird output such as a bunch of zeros. I have no idea why and any help would be greatly appreciated.
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
typedef struct _thread_data_t {
int tid;
} thread_data_t;
void *runner(void *param);
int main(int argc, char *argv[]) {
pthread_t thr[argc];
pthread_attr_t attr;
int i, rc;
//int *primeFactor;
//primeFactor = (int *)malloc(sizeof(int)*argc);
//thread_data_t thr_data[argc];
printf("Prime Numbers: ");
//Get the default attributes
pthread_attr_init(&attr);
//creat the thread
for(i = 1; i < argc; ++i){
//thr_data[i].tid = i;
if ((rc = pthread_create(&thr[i],&attr,runner,argv[i]))){
fprintf(stderr, "error: pthread_create, rc: %d\n", rc);
return EXIT_FAILURE;
}
}
//Wait for the thread to exit
for(i = 1; i < argc; ++i){
void *returnValue;
int r = 0;
int x = (sizeof(returnValue) / sizeof(returnValue[0])) - 1;
pthread_join(thr[i], &returnValue);
for(r = 0; r < x; r++){
//int c = (int *)returnValue[r];
printf("%d ", ((int *)returnValue)[r]);
}
}
printf("\nComplete\n");
}
//The Thread will begin control in this function
void *runner(void *param) {
int *primeFactors;
int num = atoi(param);
primeFactors = (int *)malloc(sizeof(int)*num);
int i, j, isPrime;
int k = 0;
for(i=2; i<=num; i++)
{
if(num%i==0)
{
isPrime=1;
for(j=2; j<=i/2; j++)
{
if(i%j==0)
{
isPrime = 0;
break;
}
}
if(isPrime==1)
{
primeFactors[k] = i;
k++;
}
}
}
//Exit the thread
// pthread_exit(0);
// pthread_exit((void *)primeFactors);
pthread_exit(primeFactors);
}