混合声明和代码
当我使用“gcc -o dene -Wall -ansi -pedantic-errors dene.c”编译函数时 ,gcc 不会发出错误。(你能在 if 循环中查看以 char .... 开头的行吗?)
static void remove_negation(char *s,char *s1)
{
char **cmainp=malloc(sizeof(char*)*1);
int len=0;int d=0; int i=0;
cmainp[0]=malloc(sizeof(char)*300);
len=strlen(s);
for(i=0;i<len;++i)
{ if(s[i]=='-')
if(i==0 || s[i-1]==',')
/*look*/ {char *p=malloc(sizeof(char)*3); /*look*/
++i; p[0]=s[i]; p[1]='\0';
strcat(s1,","); strcat(s1,p); free(p);
continue;
}
cmainp[0][d]=s[i];
++d;
} cmainp[0][d+1]='\0';
strcpy(cmainp[0],s);
free(cmainp[0]);
}
但是,当编译上面的函数时,会使用 gcc 重新格式化 ,gcc 发出该错误;
“dene.c:10:错误:ISO C90 禁止混合声明和代码”
static void remove_negation(char *s,char *s1)
{
char **cmainp=malloc(sizeof(char*)*1);
/*look*/ cmainp[0]=malloc(sizeof(char)*300); /*look*/
int len=0;int d=0; int i=0;
len=strlen(s);
for(i=0;i<len;++i)
{ if(s[i]=='-')
if(i==0 || s[i-1]==',')
{char *p=malloc(sizeof(char)*3);
++i; p[0]=s[i]; p[1]='\0';
strcat(s1,","); strcat(s1,p); free(p);
continue;
}
cmainp[0][d]=s[i];
++d;
} cmainp[0][d+1]='\0';
strcpy(cmainp[0],s);
free(cmainp[0]);
}
最后一个,gcc 发出以下错误
dene.c:16:错误:'char'之前的预期表达式
dene.c:20:错误:'p1'未声明(在此函数中首次使用)
dene.c:20: error: (每个未声明的标识符仅报告一次
dene.c:20: error: 对于它出现的每个函数。)
static void remove_negation(char *s,char *s1)
{
char **cmainp=malloc(sizeof(char*)*1);
int len=0;int d=0; int i=0;
cmainp[0]=malloc(sizeof(char)*300);
len=strlen(s);
for(i=0;i<len;++i)
{ if(s[i]=='-')
/*look*/ char *p=malloc(sizeof(char)*3); /*look*/
if(i==0 || s[i-1]==',')
{
++i; p[0]=s[i]; p[1]='\0';
strcat(s1,","); strcat(s1,p); free(p);
continue;
}
cmainp[0][d]=s[i];
++d;
} cmainp[0][d+1]='\0';
strcpy(cmainp[0],s);
free(cmainp[0]);
}
问题是为什么它们之间存在差异。
When I compile function with "gcc -o dene -Wall -ansi -pedantic-errors dene.c"
,gcc emits no error.(can you look a line which starts with char ....,in if loop,)
static void remove_negation(char *s,char *s1)
{
char **cmainp=malloc(sizeof(char*)*1);
int len=0;int d=0; int i=0;
cmainp[0]=malloc(sizeof(char)*300);
len=strlen(s);
for(i=0;i<len;++i)
{ if(s[i]=='-')
if(i==0 || s[i-1]==',')
/*look*/ {char *p=malloc(sizeof(char)*3); /*look*/
++i; p[0]=s[i]; p[1]='\0';
strcat(s1,","); strcat(s1,p); free(p);
continue;
}
cmainp[0][d]=s[i];
++d;
} cmainp[0][d+1]='\0';
strcpy(cmainp[0],s);
free(cmainp[0]);
}
But,when compile above function being reformatted with gcc
,gcc emits that error;
"dene.c:10: error: ISO C90 forbids mixed declarations and code"
static void remove_negation(char *s,char *s1)
{
char **cmainp=malloc(sizeof(char*)*1);
/*look*/ cmainp[0]=malloc(sizeof(char)*300); /*look*/
int len=0;int d=0; int i=0;
len=strlen(s);
for(i=0;i<len;++i)
{ if(s[i]=='-')
if(i==0 || s[i-1]==',')
{char *p=malloc(sizeof(char)*3);
++i; p[0]=s[i]; p[1]='\0';
strcat(s1,","); strcat(s1,p); free(p);
continue;
}
cmainp[0][d]=s[i];
++d;
} cmainp[0][d+1]='\0';
strcpy(cmainp[0],s);
free(cmainp[0]);
}
And last one,gcc emits following errors
dene.c:16: error: expected expression before ‘char’
dene.c:20: error: ‘p1’ undeclared (first use in this function)
dene.c:20: error: (Each undeclared identifier is reported only once
dene.c:20: error: for each function it appears in.)
static void remove_negation(char *s,char *s1)
{
char **cmainp=malloc(sizeof(char*)*1);
int len=0;int d=0; int i=0;
cmainp[0]=malloc(sizeof(char)*300);
len=strlen(s);
for(i=0;i<len;++i)
{ if(s[i]=='-')
/*look*/ char *p=malloc(sizeof(char)*3); /*look*/
if(i==0 || s[i-1]==',')
{
++i; p[0]=s[i]; p[1]='\0';
strcat(s1,","); strcat(s1,p); free(p);
continue;
}
cmainp[0][d]=s[i];
++d;
} cmainp[0][d+1]='\0';
strcpy(cmainp[0],s);
free(cmainp[0]);
}
question is why there are differences between them.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在 K&R 和 ANSI c 中,必须始终将声明放在作用域块的开头。 c99 中放宽了这一要求。
那么,什么是范围块?由
{
和}
分隔的区域。因此,在上面的示例中,声明
是可以的,因为它立即发生在
{
之后,而失败是因为分配位于
{
和第二个声明 (int len =0;
)。In K&R and ANSI c, you must always put declarations at the start of a scope block. This requirement is relaxed in c99.
So, whats a scope block? A region delimited by
{
and}
.So in you above example the declaration
is OK because it occurs immediately after a
{
, whilefails because the assigment comes between the
{
and the second declaration (int len=0;
).问题是你的标志“-ansi”强加了 ANSI C 的规则,其中声明只能出现在块的开头,而不是与其他语句交错的任何其他地方。
请注意,在第一个示例中,if 后面的左大括号开始一个新块,因此在那里声明新变量是合法的。在第二个示例中,您在与“char **cmainp”相同的块中声明“int len”,但在分配给“cmainp”之后。如果将赋值放在声明之后,那么一切都会好起来,因为声明将位于块的开头。
The problems is that your flag "-ansi" imposes the rules of ANSI C, among which is that declarations may appear only at the beginning of a block and not anywhere else interleaved with other statements.
Note that in your first example, the opening brace
{
after the if begins a new block, and hence it is legal to declare new variables there. In your second example, you declare "int len" in the same block as "char **cmainp", but after an assignment to "cmainp". If you place the assignment after the declaration, then everything will be fine, since the declaration would then be at the start of a block.如果出现“dene.c:10: error: ISO C90 forbids mix statements and code”错误,您将在代码中间定义
char *p=malloc(sizeof(char)*3)
。 ANSI C 要求声明仅出现在代码块的开头,而不是其他地方。如果您将 char*p
放在代码开头,然后将*p=malloc(sizeof(char)*3)
放在第 10 行,则会出现此“错误”离开。With the "dene.c:10: error: ISO C90 forbids mixed declarations and code" error you are defining
char *p=malloc(sizeof(char)*3)
in the middle of the code. ANSI C requires that the declaration appear only at the beginning of the block of code and nowhere else. If instead you put char*p
at the beginning of the code and then*p=malloc(sizeof(char)*3)
on line 10 this "error" would go away.在做任何事情之前你必须先做声明。
you have to do the declarations before you do anything.