0

I am trying to find the prime factorization of multiple numbers. If the user types 15 80 77 it will create a thread for each input and have the thread return a array of the factorization which will then be printed. However I am recieving two errors. One that says error: dereferencing 'void *' pointer [Werror] printf("%d ", returnValue[r]);

and one that says error: invalid use of void expression printf("d ", returnValue[r]);

I am not crazy familiar with pointers. Any help is greatly appreciated. Also this is my first question so bear with me.

#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 = 0; 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 = 0; i<argc; ++i){
        void *returnValue;
        int r = 0;
        pthread_join(thr[i], &returnValue);
        for(r = 0; r < sizeof(returnValue); r++){
            printf("%d ", 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);
}
Yuankun
  • 6,875
  • 3
  • 32
  • 34
  • you must cast a `void*` to it's actual type (in this case `int*`) before you can dereference it – yano Mar 05 '18 at 03:18
  • I am sorry I am not very familiar with how that works. Mind showing what that would look like? –  Mar 05 '18 at 03:25

1 Answers1

0

You are making a couple of mistakes in your code snippet.

  1. The argc in main function contains the number of command line arguments, including the script name. So you don't want to parse the first command line argument as an integer value.
  2. The sizeof operator in for(r = 0; r < sizeof(returnValue); r++) gives you the byte count of the returnValue variable, which should always be 8 in 64-bit OS because it is a pointer value. Use some other way to get the size of the resulting array.
  3. The warning you're getting is because of type misuse. Make an explicit type casting to fix it.
Yuankun
  • 6,875
  • 3
  • 32
  • 34
  • Thank you for your feedback I really appreciate it. I am unsure of how to complete step three. Is there anyway you could go a little more into that as I have not dealt with C very much. Thanks so much in advance. –  Mar 05 '18 at 03:46
  • Making type casting is easy: replace `printf("%d ", returnValue[r]);` to `printf("%d ", ((int *)returnValue)[r]);`. This tricks the compiler to treat `returnValue` as an integer pointer. – Yuankun Mar 05 '18 at 03:51