mixed declarations and codes
Posted
by gcc
on Stack Overflow
See other posts from Stack Overflow
or by gcc
Published on 2010-05-24T10:17:21Z
Indexed on
2010/05/24
10:21 UTC
Read the original article
Hit count: 281
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);
/*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]=='-')
/*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.
© Stack Overflow or respective owner