【发布时间】:2020-11-04 04:25:23
【问题描述】:
我正在尝试将结构的实例传递给线程,但由于某种原因,它正在打印整数的随机值,但双精度值的正确值?
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
//Pass an integer value and a double value to a thread (use struct!)
typedef struct param {
int val;
double db;
}param_t;
void *myth(void *arg)
{
param_t myT, *myPt;
myT = *((param_t*)arg);
myPt = (param_t*)arg;
printf("%d\n", myT.val);
printf("%.3lf\n", myPt->db);
pthread_exit(NULL);
}
void main()
{
pthread_t tid;
int i = 3733;
double d = 3733.001;
param_t t_struct;
param_t *p;
p = malloc(sizeof(param_t));
*p = t_struct;
t_struct.val = i;
t_struct.db = d;
pthread_create(&tid, NULL, myth, (void *)&p);
pthread_join(tid, NULL);
return;
}
输出: 10969104 3733.001
【问题讨论】: