无效的二进制操作数*(具有'ab {aka struct a}'和'ab * {aka struct a *}') [英] Invalid operands to binary * (have ‘ab {aka struct a}’ and ‘ab * {aka struct a *}’)

查看:388
本文介绍了无效的二进制操作数*(具有'ab {aka struct a}'和'ab * {aka struct a *}')的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个程序来交换数组中的两个结构,我的编码如下

I wrote a program to swap two structures in an array and my coding is as follows

#include <stdio.h>
struct a {
    char *name;
    int id;
    char *department;
    int num;
};
typedef struct a ab;

void swap(ab *, ab *);

int main(int argc, char *argv[])
{
    ab array[2] = {{"Saud", 137, "Electronics", 500}, {"Ebad", 111, "Telecom", 570}};
    printf("First student data:\n%s\t%d\t%s\t%d", array[0].name, array[0].id,
           array[0].department, array[0].num);

    printf("\nSecond Student Data\n%s\t%d\t%s\t%d\n", array[1].name, array[1].id,
           array[1].department, array[1].num);

    swap(&array[0], &array[1]);
    // printf("")
    return 0;
}

void swap(ab *p, ab *q){
    ab tmp;
    tmp = *p
    *p = *q;
    *q = tmp;
}

编译时会出现错误,

newproject.c:在交换"功能中:
newproject.c:26:3:错误:无效的二进制操作数*(具有'ab {aka
struct a}’和‘ab * {aka struct a *}’)
* p = * q;

newproject.c: In function ‘swap’:
newproject.c:26:3: error: invalid operands to binary * (have ‘ab {aka
struct a}’ and ‘ab * {aka struct a *}’)
*p=*q;

怎么了?

推荐答案

第26行(上一行)的末尾缺少分号.

There's a missing semicolon at the end of line 26 (the previous line).

tmp=*p

由于这个原因,编译器认为下一行是同一条语句的一部分,这意味着整个语句将被解释为:

Due to this, the compiler considers the next line to be part of the same statement, meaning that the entire statement is interpreted as:

tmp=*p * p = *q;

第二个*被视为两个操作数的乘积-*pp-这是错误消息来自的地方:

The second * is seen as a multiply of two operands - *p and p - which is where the error message comes from:

无效的操作数二进制* (具有'ab {aka struct a}’和‘ab * {aka struct a *}’)

invalid operands to binary * (have ‘ab {aka struct a}’ and ‘ab * {aka struct a *}’)

(因为*pab类型,而pab *类型).

(Because *p is of type ab, and p is of type ab *).

这篇关于无效的二进制操作数*(具有'ab {aka struct a}'和'ab * {aka struct a *}')的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆