我收到了这个错误。请帮忙!
3.c:在函数main中:
3.c:92:错误:从类型prcmd_t分配类型struct prcmd_t *时出现不兼容的类型
#include <stdio.h>
#include <pthread.h>
#include <time.h>
#include <stdlib.h>
typedef struct pr_struct{
int owner;
int burst_time;
struct pr_struct *next_prcmd;
} prcmd_t;
static prcmd_t *pr_head = NULL;
static prcmd_t *pr_tail = NULL;
static int pending_request = 0;
static pthread_mutex_t prmutex = PTHREAD_MUTEX_INITIALIZER;
int add_queue(prcmd_t *node)
{ pthread_mutex_lock(&prmutex);
//code
prcmd_t *curNode = pr_head;
if(pr_head == NULL) { pr_head = node; return;}
while(curNode->next_prcmd)
{
curNode = curNode->next_prcmd;
}
curNode->next_prcmd = node;
//
pending_request++;
pthread_mutex_unlock(&prmutex);
return(0);
}
**//inprogress
int remove_queue(prcmd_t **node)
{
printf("prenull");
pthread_mutex_lock(&prmutex);
prcmd_t *tempNode;
if(node == NULL)
{
//your code
printf("Queue is empty");
//
pthread_mutex_unlock(&prmutex);
return(-1);
}
else
{
printf("in else");
//your code
tempNode =(*node)->next_prcmd;
free(node);
//
pending_request--;
pthread_mutex_unlock(&prmutex);
return(0);
}
}
//end progress**
int main()
{
if (pr_head == NULL)
{
printf("List is empty!\n\n");
}
int i=0;
int length = 4;
prcmd_t *pr[length];
for(i =0;i<length;i++)
{
pr[i] = (prcmd_t*)malloc(sizeof(prcmd_t));
pr[i]->owner = i+1;
pr[i]->burst_time = i + 2;
add_queue(pr[i]);
}
prcmd_t *curNode = pr_head;
while(curNode)
{
printf("%i %i\n", curNode->owner,curNode->burst_time);
curNode = curNode->next_prcmd;
}
**//something is messed up here i think.
curNode = *pr_head;
remove_queue(&curNode);
//**
while(curNode)
{
printf("%i %i\n", curNode->owner,curNode->burst_time);
curNode = curNode->next_prcmd;
}
}
答案 0 :(得分:1)
curNode的类型为prcmd_t *
(指向prcmd_t的指针),而pr_head
的类型为prcmd_t *
,因此它们处于相同的引用级别,但您尝试分配值由pr_head
(类型为prcmd_t
)指向curNode
(类型为prcmd_t *
),这是不兼容的。
我不清楚你要做什么,但正确的语法是
curNode = pr_head;
或
memcpy(curNode, pr_head, sizeof(prcmd_t));