我正在創建一個線程並將指針傳遞給它。 但是,當我投這個指針,因爲它應該是一個(int *)我有一個分段錯誤。C - pthread分段錯誤11
int *ptr = (int *)ptrTotal2;
下面是代碼:
void *firstCalc(void *ptrTotal2){
int valA = 1;
int valB = 2;
int *ptr = (int *)ptrTotal2;
*ptr = valA + valB;
printf("Value of valA = %d\nValue of valB = %d\n", valA, valB);
printf("Value of subtotal *ptrTotal1 = %d\n", *ptr);
pthread_exit(NULL);
}
int main(int argc, char **argv) {
pthread_t thread1;
int *ptrTotal2 = 0;
int iret1;
iret1 = pthread_create(&thread1, NULL, firstCalc, (void*) ptrTotal2);
if(iret1){
fprintf(stderr,"Error - pthread_create() return code: %d\n",iret1);
exit(EXIT_FAILURE);
}
pthread_join(thread1, NULL);
exit(EXIT_SUCCESS);
}
感謝它幫助了很多;) –