Hey,
as an experiment and an exercise I try to create a program which does two things at the same time:
The first thread shall wait for users input an override an int array.
The other thread shall constantly sum up the values every 30s.
Code:
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void job(int nums[]){
while(1){
int sum = 0;
int length = (int)(sizeof(nums)/sizeof(nums[0])); //Compiler Warning Here
for(int i = 0; i < length; i++){
sum += nums[i];
}
printf("Length is %d", sum/length);
sleep(30); //Compiler Warning here
}
}
int main(int argc,char **argv){
pthread_t *calc;
int counter = 0;
int nums[20];
pthread_create(calc, NULL, *job, &nums); //Compiler Warning here
while(1){
char buf[50];
fgets(buf, 50, stdin);
nums[counter++%20] = strtol(buf, NULL ,10);
}
printf("Unreachable code");
}
Unfortunately this does not run properly.
I am explicty wondering about the first warning and the third.
Solution:
The error was here:
Code:
pthread_t *calc;
int counter = 0;
int nums[20];
pthread_create(calc, NULL, *job, &nums); //Compiler Warning here
Thread closed