How would I deference my argument to get the integer value in my print statement and also free the pointer inside the function?
This is a simpler version of what I'm running into with a project I'm working on:
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
void* my_Func(void *received_intPointer){
int val = *(int*) received_intPointer;
printf("value: %d \n", val);
free(received_intPointer);
}
int main(){
int array[5];
int *ptr[5];
for(int i=0; i<5; i++){
ptr[i] = (int*) malloc(sizeof(int));
}
printf("\nEnter 5 numbers:\n");
for (int i=0; i<5; i++){
scanf("%d", &array[i]);
ptr[i] = &array[i];
}
pthread_t tid[5];
for(int i=0; i<5; i++){
pthread_create(&(tid[i]), NULL, my_Func, &ptr[i]);
}
for (int i=0; i<5; i++){
pthread_join(tid[i], NULL);
}
}