我刚刚开始使用Visual Studio(我从dreamspark获得了VS 2012,并且我已经开始使用Windows了很长时间)并且我遇到了一些麻烦.
我的Source Files文件夹下有一个名为"main.c"的文件,如下所示:
#include <stdio.h>
typedef struct S_s S;
struct S_s {
void* x;
};
int main(int argc, char** argv)
{
int N;
scanf("%d", &N);
S* s;
printf("%p", s);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
当我尝试构建它时,给出了以下错误消息:
Error 3 error C2065: 's' : undeclared identifier c:\users\math4tots\documents\visual studio 2012\projects\algorithms\lecture1\main.c 13 1 Lecture1
Error 4 error C2065: 's' : undeclared identifier c:\users\math4tots\documents\visual studio 2012\projects\algorithms\lecture1\main.c 14 1 Lecture1
Error 2 error C2275: 'S' : illegal use of this type as an expression c:\users\math4tots\documents\visual studio 2012\projects\algorithms\lecture1\main.c 13 1 Lecture1
Run Code Online (Sandbox Code Playgroud)
有趣的是,如果我注释掉它,它就会很好scanf.
我确实在"Visual C++"选项下创建了一个"空项目",所以我不确定VS是否将其编译为C或C++程序.但是,我的印象是我的代码与C/C++无关,它应该在C或C++中编译.
在保持程序语义的同时,我还能做些什么来构建它?
Joh*_*åde 14
代码是合法的C++和合法的C99,但不是合法的C89.C89中的变量声明必须出现在块的开头,因此在C89中使用S* s;after之后scanf("%d", &N);
就不行了.
没有尝试过,但旧的C规则(在C99之前)只允许在块的开头声明自动变量.因此,根据这些规则,干预scanf使声明成为S* s非法.评论出scanf"修复"问题.这在C++中一直是合法的.