mixed declarations and codes

user319824 picture user319824 · May 24, 2010 · Viewed 19.4k times · Source

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.

Answer

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

{
   char *p=malloc(sizeof(char)*3); /* ...

is OK because it occurs immediately after a {, while

{
  char **cmainp=malloc(sizeof(char*)*1);    

  /*look*/ cmainp[0]=malloc(sizeof(char)*300); /*look*/

  int len=0;...

fails because the assigment comes between the { and the second declaration (int len=0;).