我对 C 语言相当陌生。我写了一段代码,它创建了两个线程来计算两个不同的结果。代码实际上可以工作,但编译显示错误,我想知道实际上是什么错误。谁能帮我?我是C新手,所以我想这可能是一个非常愚蠢的错误......
错误:
q3.c: In function ‘main’:
q3.c:63:2: warning: passing argument 3 of ‘pthread_create’ from incompatible pointer type [enabled by default]
In file included from q3.c:5:0:
/usr/include/pthread.h:225:12: note: expected ‘void * (*)(void *)’ but argument is of type ‘double * (*)(void *)’
q3.c:64:9: warning: passing argument 3 of ‘pthread_create’ from incompatible pointer type [enabled by default]
In file included from q3.c:5:0:
/usr/include/pthread.h:225:12: note: expected ‘void * (*)(void *)’ but argument is of type ‘double * (*)(void *)’
编码:
#include <stdlib.h>
#include <pthread.h>
#include <string.h>
#include<stdio.h>
#include<math.h>
#define PI 3.1415
int i, x, n;
double *sin_x(void* dimension);
double *cos_x(void* dimension);
int fact(int);
struct data
{
int ang_deg;
int no_of_terms;
};
int fact(int num)
{
int f = 0;
if (num == 1)
return 1;
else
f = num * fact(num - 1);
return f;
}
int main(int argc, char argv[])
{
printf("\nEnter x and n:\t");
scanf("%d %d", &x, &n);
struct data
{
int ang_rad;
int no_of_terms;
};
struct data dimension; // automatic allocation, all fields placed on stack
dimension.ang_rad = x;
dimension.no_of_terms = n;
pthread_t thrd1, thrd2;
int thret1, thret2;
thret1 = pthread_create(&thrd1, NULL, sin_x, (void *) &dimension);
thret2 = pthread_create(&thrd2, NULL, cos_x, (void *) &dimension);
pthread_join(thrd1, NULL );
pthread_join(thrd2, NULL );
//printf("\nthret1 = %d\n", thret1);
//printf("thret2 = %d\n", thret2);
sleep(5);
printf("Parent Thread exiting...\n");
exit(1);
return 0;
}
double *sin_x(void* dimension)
{
struct data* dim = (struct data*) dimension;
int ang_deg = dim->ang_deg;
int no_of_terms = dim->no_of_terms;
//int ang_deg, int no_of_terms
int term, j;
double value = 0.0, ang_rad = 0.0;
ang_rad = (double) ang_deg * PI / 180;
for (term = 1, j = 2; term < no_of_terms * 2; term += 2, j++)
{
value += (double) pow(-1.0, j) * pow(ang_rad, term) / fact(term);
}
printf("\nSin(%d) = %f", ang_deg, value);
double *a = &value;
return a;
}
double *cos_x(void* dimension)
{
struct data* dim = (struct data*) dimension;
int ang_deg = dim->ang_deg;
int no_of_terms = dim->no_of_terms;
int term, j;
double value = 1.0, ang_rad = 0.0;
ang_rad = (double) ang_deg * PI / 180;
for (term = 2, j = 1; term <= no_of_terms; term += 2, j++)
{
value += (double) pow(-1.0, j) * pow(ang_rad, term) / fact(term);
}
printf("\nCos(%d) = %f", ang_deg, value);
double *a = &value;
return a;
}