对于许多问题,答案似乎可以在"标准"中找到.但是,我们在哪里找到它?最好是在线.
谷歌搜索有时会觉得徒劳,尤其是对于C标准,因为他们在编程论坛的大量讨论中被淹没.
要开始这个,因为这些是我现在正在搜索的,那里有很好的在线资源:
在使用指针时,我编写了以下代码,
int main()
{
int a[]={10,20,30,40,50};
int i;
for(i=0;i<5;i++)
{
printf("\n%d",*a);
a++;
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
现在根据我的理解,数组名称本身是c中的地址,根据我的知识,完成的指针算法是正确的.但是,当我尝试运行代码时,它给了我"Lvalue Required"错误.
那么出现Lvalue所需错误的确切原因是什么呢?因为在此之前我也遇到过这种错误的情况.其次,为什么指针的算术在这种情况下不合法呢?
我一直试图通过编写简单的代码来理解指针的概念,但遇到了一个错误问题,似乎我无法解决或理解它。
#include <stdio.h>
int *foo(void);
int main(void) {
printf("%d\n", *foo());
return 0;
}
int *foo(void) {
static int num = 1;
++num;
return &(++num);
}
Run Code Online (Sandbox Code Playgroud)
这是错误消息。
error: lvalue required as unary ‘&’ operand
return &(++num);
Run Code Online (Sandbox Code Playgroud)
函数'foo()'返回一个指向int的指针,并且main应该使用*运算符来打印返回的int。对于foo()中的静态num,我认为通过放置静态限定符,num不再是临时变量,因此'&'可用于num。