I have a segmentation fault error in my program. I am practising with multithreading programs in POSIX C. I run these programs in FREEBSD system.
Here is my code:
#include <stdlib.h>
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#include <sys/types.h>
#define NUM_THREADS 15
void *PrintHello(void *threadid)
{
printf("Hello World from thread: %p \n", threadid);
fflush(NULL);
pthread_exit(NULL);
}
int main(int argc, char *argv[])
{
int rtrn, *i;
pthread_t threads[NUM_THREADS];
for((*i)=0; (*i)<NUM_THREADS; (*i)++)
{
if( pthread_create(&threads[(*i)], NULL, PrintHello, (void *)i) != 0)
{
printf("Error code %d detected while thread %d was being created.", rtrn, *i);
}
}
return 0;
}
I had to turn i integer variable into pointer integer because this solved a error during compiling (line 22: warning: cast to pointer from integer of different size)
The compile error has been resolved but now when I run the program, it shows me this: Segmentation fault: 11 (core dumped)
Probably the previous warning has not been resolved but I don't know how to fix it...