简体   繁体   English

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

[英]Invalid operands to binary * (have ‘ab {aka struct a}’ and ‘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;
}

On compiling it it gives an error, 在编译它时会出错,

newproject.c: In function 'swap': newproject.c:在函数'swap'中:
newproject.c:26:3: error: invalid operands to binary * (have 'ab {aka newproject.c:26:3:错误:无效操作数到二进制*(有'ab {aka
struct a}' and 'ab * {aka struct a *}') struct a''和'ab * {aka struct a *}')
*p=*q; * P = * Q;

What is the mistake? 这是什么错误?

There's a missing semicolon at the end of line 26 (the previous line). 在第26行(前一行)的末尾有一个缺少的分号。

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;

The second * is seen as a multiply of two operands - *p and p - which is where the error message comes from: 第二个*被视为两个操作数的乘法 - *pp - 这是错误消息的来源:

invalid operands to binary * (have 'ab {aka struct a}' and 'ab * {aka struct a *}') 无效的二进制操作数* (有'ab {aka struct a}'和'ab * {aka struct a *}')

(Because *p is of type ab , and p is of type ab * ). (因为*p的类型为ab ,而p的类型为ab * )。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将错误无效操作数编译为二进制 &amp;&amp;(具有 'int' 和 'pthread_t' {aka 'struct<anonymous> '})</anonymous> - Compiling error invalid operands to binary && (have 'int' and 'pthread_t' {aka 'struct <anonymous>'}) 二进制 + 的无效操作数(具有 'struct student' 和 'int') - invalid operands to binary + (have 'struct student' and 'int') 二进制操作数无效!=(具有结构和 void *) - Invalid operands to binary != (have struct and void *) 错误:“-&gt;”的类型参数无效(具有&#39;Queue {aka struct Queue} - error:invalid type argument of '->'(have 'Queue {aka struct Queue} 错误:对二进制%无效的操作数(具有“结构分数*”和“结构分数*”) - error: invalid operands to binary % (have ‘struct Fraction *’ and ‘struct Fraction *’) 名为“ AB”的结构体可以包含AB数组吗? - Can a Struct Named “AB” Contain an Array of AB? 错误:二进制&lt;&lt;的无效操作数(具有“ struct str *”和“ int”) - error: invalid operands to binary << (have ‘struct str *’ and ‘int’) 无效使用不完整类型&#39;PGconn {aka struct pg_conn}&#39; - invalid use of incomplete type 'PGconn {aka struct pg_conn}' 二进制表达式的无效操作数(&#39;struct node&#39;和&#39;struct node&#39;) - invalid operands to binary expression ('struct node ' and 'struct node ') 在 C 中重新创建 strlcat 函数“二进制表达式的无效操作数(&#39;size_t&#39;(又名&#39;unsigned long&#39;)和&#39;char *&#39;)” - Recreating strlcat function in C "invalid operands to binary expression ('size_t' (aka 'unsigned long') and 'char *')"
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM