C “二进制表达式的操作数无效”错误
我不断地发现这个错误: 二进制表达式“int”和“Primenumber”的操作数无效 “结构编号” 在下面我用**标记的两行上。出了什么问题,我该如何解决?该代码用于数据结构赋值C “二进制表达式的操作数无效”错误,c,C,我不断地发现这个错误: 二进制表达式“int”和“Primenumber”的操作数无效 “结构编号” 在下面我用**标记的两行上。出了什么问题,我该如何解决?该代码用于数据结构赋值 typedef struct number { int num[3]; } Primenumber; typedef struct node { int data; struct node *next; } Node; Node *head = NULL; int AddPrimeNum
typedef struct number
{
int num[3];
} Primenumber;
typedef struct node
{
int data;
struct node *next;
} Node;
Node *head = NULL;
int AddPrimeNumber(Primenumber x)
{
Node *n;
Node *newNode;
//Create a new node
newNode = (Node*)malloc(sizeof(Node));
**newNode->data=x;**
newNode->next=NULL;
if (head == NULL)
{
head = newNode;
}
else
{
n= head;
while (n-> next != NULL)
{
n= n->next;
}
n->next= newNode;
}
return 0;
}
int SearchPrimeNumber(Primenumber x)
{
int pos=0;
Node *n = head;
while (n != NULL)
{
**if (n->data ==x)**
{
return pos;
}
else
{
pos++;
n= n->next;
}
}
return 0;
}
int DisplayPrimeNumber()
{
Node *n =head;
while (n != NULL)
{
printf("%d -> ", n->data);
n= n->next;
}
printf("\n");
return 0;
}
第一次
newNode->data=x;
将PrimeNumber类型的结构赋值给int
第二次比较PrimeNumber类型的结构和int时
两者都是错的,可能是你想要的
typedef struct Node {
PrimeNumber data;
struct Node *next;
};
赋值部分可以,但你必须详细说明比较部分,我将使用一个函数
areEqualPrimeNumbers(PrimeNumber *x, PrimeNumber *y)
{
return ((x->num[0] == y->num[0]) && (x->num[1] == y->num[1]) && (x->num[2] == y->num[2]));
}
或者如果您想使用memcmp
然后
areEqualNodes(&x, &(n->data));
memcmp版本更好,因为它不依赖于PrimeNumber的定义。newNode->data的类型是int,而x的类型是PrimeNumber struct number。除了赋值,C对整个结构不提供任何操作。在第一个**s中,您试图将Primenumber类型的x赋值给n->int类型的数据;这是你的第一个错误
typedef struct number
{
int num[3];
} Primenumber;
typedef struct node
{
int data;
struct node *next;
} Node;
Node *head = NULL;
int AddPrimeNumber(Primenumber x)
{
Node *n;
Node *newNode;
//Create a new node
newNode = (Node*)malloc(sizeof(Node));
**newNode->data=x;**
newNode->next=NULL;
if (head == NULL)
{
head = newNode;
}
else
{
n= head;
while (n-> next != NULL)
{
n= n->next;
}
n->next= newNode;
}
return 0;
}
int SearchPrimeNumber(Primenumber x)
{
int pos=0;
Node *n = head;
while (n != NULL)
{
**if (n->data ==x)**
{
return pos;
}
else
{
pos++;
n= n->next;
}
}
return 0;
}
int DisplayPrimeNumber()
{
Node *n =head;
while (n != NULL)
{
printf("%d -> ", n->data);
n= n->next;
}
printf("\n");
return 0;
}
在第二个**中,您尝试相同的方法进行比较;这是你的第二个错误
并且,请使用简单的//错误注释标记错误,不要使用**s 你想做什么?为什么素数是一个三位数的东西?你在比较一个标量int newNode->data和一个结构素数x。您的意思是newNode->data=x.num[0]?您的意思可能是typedef struct node{PrimeNumber data;struct node*next;}node;?谢谢,我会记在心里添加评论!错误已被分类,程序运行良好,再次感谢您的帮助。
// always comment your code so others (or yourself later)
// do not have to 'reverse engineer' it
// <-- declutter code by just defining a struct type, not typedef struct
struct PrimeNumber
{
int num[3];
};
struct Node
{
int data;
struct node *next;
};
// <-- due to better definition of struct, need to use the 'struct' modifier
struct Node *head = NULL;
// <-- pass as pointer so compiler does not generate two hidden calls to memcpy())
// <-- nor allocate memory space that is unusable for anything else
//int AddPrimeNumber(PrimeNumber x)
// <-- due to better definition of struct, need to use the 'struct' modifier
int AddPrimeNumber(struct PrimeNumber* x)
{
// <-- due to better definition of struct, need to use the 'struct' modifier
// <-- initialize local variables to a 'safe' value
struct Node *n = NULL;
struct Node *newNode = NULL;
//Create a new node
// <-- always check the returned value from malloc() to assure operation successful
if( NULL == (newNode = malloc(sizeof(Node)) ) )
{ // then malloc failed
perror( "malloc failed" );
exit( EXIT_FAILURE );
}
// implied else, malloc successful
// <-- x contains 3 integer fields, newNode contains 1 integer field.
// <-- what were you expecting to happen?
// <-- perhaps you meant: newNode->data = x->num[0]; which only copies one int, not all three
**newNode->data=x;**
newNode->next=NULL;
if (head == NULL) // this handles special case of empty list
{
head = newNode;
}
else
{ // else, list already contains one or more nodes
n= head;
while (n->next != NULL)
{
// step to next node in linked list
n= n->next;
}
// <-- currently 'n' points to last node in linked list
// <-- add new node to end of linked list
n->next= newNode;
}
return 0;
} // end function: AddPrimeNumber
// similar considerations need to be applied to the other posted function
// always comment your code so others (or yourself later)
// do not have to 'reverse engineer' it
// <-- declutter code by just defining a struct type, not typedef struct
struct PrimeNumber
{
int num[3];
};
struct Node
{
int data;
struct node *next;
};
// <-- due to better definition of struct, need to use the 'struct' modifier
struct Node *head = NULL;
// <-- pass as pointer so compiler does not generate two hidden calls to memcpy())
// <-- nor allocate memory space that is unusable for anything else
//int AddPrimeNumber(PrimeNumber x)
// <-- due to better definition of struct, need to use the 'struct' modifier
int AddPrimeNumber(struct PrimeNumber* x)
{
// <-- due to better definition of struct, need to use the 'struct' modifier
// <-- initialize local variables to a 'safe' value
struct Node *n = NULL;
struct Node *newNode = NULL;
//Create a new node
// <-- always check the returned value from malloc() to assure operation successful
if( NULL == (newNode = malloc(sizeof(Node)) ) )
{ // then malloc failed
perror( "malloc failed" );
exit( EXIT_FAILURE );
}
// implied else, malloc successful
// <-- x contains 3 integer fields, newNode contains 1 integer field.
// <-- what were you expecting to happen?
// <-- perhaps you meant: newNode->data = x->num[0]; which only copies one int, not all three
**newNode->data=x;**
newNode->next=NULL;
if (head == NULL) // this handles special case of empty list
{
head = newNode;
}
else
{ // else, list already contains one or more nodes
n= head;
while (n->next != NULL)
{
// step to next node in linked list
n= n->next;
}
// <-- currently 'n' points to last node in linked list
// <-- add new node to end of linked list
n->next= newNode;
}
return 0;
} // end function: AddPrimeNumber
// similar considerations need to be applied to the other posted function