"Invalid operands to binary expressions"错误



我不断收到此错误:

"二进制表达式('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;
}

第一次

newNode->data=x;

您正在将PrimeNumber类型的结构分配给int

第二次将PrimeNumber类型的结构与int进行比较

n->data ==x

两者都错了,可能是你想要的

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

areEqualPrimeNumbers(PrimeNumber *x, PrimeNumber *y)
{
    return (memcmp(x->num, y->num, sizeof(x->num)) == 0);
}

然后

areEqualNodes(&x, &(n->data));

memcmp版本更好,因为它不依赖于PrimeNumber的定义。

newNode->data 的类型

int,而x 的类型为 Primenumberstruct number )。C 对整个结构不提供除赋值之外的操作。

在第一个**s中,您尝试将类型Primenumberx分配给类型为intn->data;这是你的第一个错误。

在第二个**s中,您正在尝试相同的比较;这是你的第二个错误。

并且,请用简单的//error注释标记您的错误,而不是使用**s;)。

// 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

相关内容

最新更新