当前位置: 技术问答>linux和unix
很小的入门程序,不知哪里出错?
来源: 互联网 发布时间:2014-12-01
本文导语: #include #include typedef struct _my{ char hello[16]; }my; my MyHello; int main(void) { memset(&MyHello,0,sizeof(my)); FILE *fp=NULL; fp=fopen("./hello.c","rb"); strcpy(MyHello.hello,"hello w...
#include
#include
typedef struct _my{
char hello[16];
}my;
my MyHello;
int main(void)
{
memset(&MyHello,0,sizeof(my));
FILE *fp=NULL;
fp=fopen("./hello.c","rb");
strcpy(MyHello.hello,"hello worldn");
printf(MyHello.hello);
fclose(fp);
return 0;
}
错误:
test.c: In function `main':
test.c:13: parse error before `*'
test.c:14: `fp' undeclared (first use in this function)
test.c:14: (Each undeclared identifier is reported only once
test.c:14: for each function it appears in.)
如果把memset拿掉就没错误了
#include
typedef struct _my{
char hello[16];
}my;
my MyHello;
int main(void)
{
memset(&MyHello,0,sizeof(my));
FILE *fp=NULL;
fp=fopen("./hello.c","rb");
strcpy(MyHello.hello,"hello worldn");
printf(MyHello.hello);
fclose(fp);
return 0;
}
错误:
test.c: In function `main':
test.c:13: parse error before `*'
test.c:14: `fp' undeclared (first use in this function)
test.c:14: (Each undeclared identifier is reported only once
test.c:14: for each function it appears in.)
如果把memset拿掉就没错误了
|
把13行放到memset前面就可以了。
linux里的c中,变量声明一定要在执行语句的前面。
linux里的c中,变量声明一定要在执行语句的前面。
|
错了错了,楼上说得对,实践证明,楼上对
|
memset((char *)&MyHello,0,sizeof(my));