gpt4 book ai didi

c - Dev-C++ 给出 '&' 引用语法错误

转载 作者:太空宇宙 更新时间:2023-11-04 05:20:46 25 4
gpt4 key购买 nike

Dev-C++ 有什么问题,或者我的代码中有关于使用引用变量的错误?

#include <stdio.h>

struct P {
int x;
};

int main(int argc, char **argv){
struct P Point[5];
struct P & rPoint;

int i;
for(i=0;i<=4;i++) {
rPoint = Point[i]; // I know. I can use Point[i].x = i. But...
rPoint.x = i;
}

for(i=0;i<=4;i++) {
rPoint = Point[i];
printf("%d\n", rPoint.x);
}
system("pause");
return 0;
}

错误: 9 C:***\main.c '&' 标记前的语法错误

最佳答案

C++ 不允许未分配的引用,所以这是你的错误:

struct P & rPoint;

如果要重新分配,请使用指针。

int main(int argc, char **argv){
struct P points[5];
struct P* point;

int i;
for(i=0;i<=4;i++) {
point = points + i; // or &points[i]
point->x = i;
}
// ...

关于c - Dev-C++ 给出 '&' 引用语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9848575/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com