C 奇怪的printf/pthread错误?

C 奇怪的printf/pthread错误?,c,pthreads,printf,mutex,C,Pthreads,Printf,Mutex,因此,我正在处理一个threads项目,我正在测试我的一个文件,确保结构和字段得到正确的值。我正在运行此功能: struct ReportQueue { sem_t count; pthread_mutex_t mutexAdd; ReportList *RQList; }; ReportQueue *RQCreate() { ReportQueue *rq; printf("RQCreate() called\n"); rq = calloc(1, si

因此,我正在处理一个threads项目,我正在测试我的一个文件,确保结构和字段得到正确的值。我正在运行此功能:

struct ReportQueue {
   sem_t count;
   pthread_mutex_t mutexAdd;
   ReportList *RQList;
};

ReportQueue *RQCreate() {
   ReportQueue *rq;

   printf("RQCreate() called\n");
   rq = calloc(1, sizeof(ReportQueue));
   sem_init(&rq->count, 0, 0);
   pthread_mutex_init(&rq->mutexAdd, NULL);
   rq->RQList = NULL;
   return rq;
}
有了这一点:

int main() {
   ReportQueue *rQueue;
   Report report;

   rQueue = RQCreate();
   printf("SemCount: |%d| RQList: |%d| MutexAdd |%d|\n", rQueue->count, rQueue->RQList, rQueue->mutexAdd);
   printf("SemCount: |%d|\n", rQueue->count);
   printf("RQList: |%d|\n", rQueue->RQList);
   printf("MutexAdd: |%d|\n", rQueue->mutexAdd);

   return;
}
我得到了这个输出:

RQCreate() called
SemCount: |0| RQList: |128| MutexAdd |0|
SemCount: |0|
RQList: |0|
MutexAdd: |0|

这对我来说毫无意义。“RQList”的值不应该根据我打印它的方式而改变?我做错了什么?

一个很好的小例子。。。这不是一个答案,但表明一切似乎都很好

+这不符合评论:D
#包括
#包括
#包括
#包括
结构报告队列{
扫描电镜计数;
pthread_mutex_t mutexAdd;
int*RQList;
};
struct ReportQueue*RQCreate(){
结构报告队列*rq;
printf(“RQCreate()调用\n”);
rq=calloc(1,sizeof(struct ReportQueue));
sem_init(&rq->count,0,0);
pthread_mutex_init(&rq->mutexAdd,NULL);
rq->RQList=NULL;
返回rq;
}
int main(){
struct ReportQueue*rQueue;
rQueue=RQCreate();
printf(“SemCount:|%d | RQList:|%d |\n”,rQueue->count,(int)rQueue->RQList);
printf(“SemCount:|%d |\n”,rQueue->count);
printf(“RQList:|%d |\n”,(int)rQueue->RQList);
返回1;
}

不要使用
%d
打印类型为
sem\u t
pthread\u mutex\u t
或指针的变量。充其量,您将获得实现定义的行为(如果
sem\u t
pthread\u mutex\u t
恰好是
int
的typedef,指针恰好与int大小相同)。但一般来说,您会得到未定义的行为,因为这些类型可能与
int
无关,
%d
告诉
printf()
相应参数的类型是什么


这就是为什么
printf()
调用会产生毫无意义的输出。您正在向
printf()
传递毫无意义的参数。

你们介意帮我解决问题吗,而不是因为我不知道为什么的原因而投票否决我的问题?我认为这是一个值得提出的问题。据我所知,该值不应更改。什么是RQList,为什么是%d?它到底是十进制/整数/数字变量吗?使用%p打印指针值。鉴于我的calloc调用将所有内容初始化为0或NULL,我认为可以使用%d作为有效的格式说明符来测试这些值。RQList应该是报表列表的链接列表。我不确定sem_t和pthread_mutex_t的基本类型。IIRC,参数类型和格式说明符之间的不匹配是未定义的行为。
#include <stdlib.h>
#include <stdio.h>
#include <semaphore.h>
#include <pthread.h>

struct ReportQueue {
   sem_t count;
   pthread_mutex_t mutexAdd;
   int *RQList;
};

struct ReportQueue *RQCreate() {
   struct ReportQueue *rq;

   printf("RQCreate() called\n");
   rq = calloc(1, sizeof(struct ReportQueue));
   sem_init(&rq->count, 0, 0);
   pthread_mutex_init(&rq->mutexAdd, NULL);
   rq->RQList = NULL;
   return rq;
}

int main() {
   struct ReportQueue *rQueue;

   rQueue = RQCreate();
   printf("SemCount: |%d| RQList: |%d| \n", rQueue->count, (int)rQueue->RQList);
   printf("SemCount: |%d|\n", rQueue->count);
   printf("RQList: |%d|\n", (int)rQueue->RQList);

   return 1;
}