gpt4 book ai didi

c - 为什么此引用不起作用/无效?

转载 作者:行者123 更新时间:2023-11-30 20:45:39 27 4
gpt4 key购买 nike

我正在学习 C 语言引用,这是我的代码:

#include <stdio.h>

int main(void)
{
int x = 5;

int &ref = x;

printf("%d\n", x);
printf("%p\n", &x);
printf("%p\n", &ref);

return 0;
}

当我编译时,它显示这些错误:

reference.c:7:6: error: expected identifier or '('

int &ref = x;

reference.c:11:18: error: use of undeclared identifier 'ref'

printf("%p\n", &ref);

2 errors generated.

我应该做什么来解决这个问题并提前致谢。

最佳答案

c没有引用类型,只能使用point而不是ref。

#include <stdio.h>

int main(void) {
int x = 5;

int *ref = &x;

printf("%d\n", x);
printf("%p\n", &x);
printf("%p\n", &ref);

return 0;
}

关于c - 为什么此引用不起作用/无效?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53943940/

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