[英]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 {akanewproject.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: 第二个
*
被视为两个操作数的乘法 - *p
和p
- 这是错误消息的来源:
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.