"二进制表达式的操作数无效"错误

时间:2014-12-31 16:49:47

标签: c

我一直收到这个错误:

  

"二进制表达式的操作数无效(' int'和' Primenumber'(又名   '结构编号'))

我在下面用**标记的两行。什么是错的,我该如何解决?该代码用于数据结构分配。

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;
}

4 个答案:

答案 0 :(得分: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的定义。

答案 1 :(得分:0)

newNode->data的类型为int,而x的类型为Primenumberstruct number)。除了赋值,C不对整个结构进行操作。

答案 2 :(得分:0)

首先,您尝试将x类型Primenumber分配给n->data类型的int;这是您的第一个错误。

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

并且,请使用简单的//error条评论标记您的错误,而不是使用**;)。

答案 3 :(得分:0)

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