views:

192

answers:

3

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.

+3  A: 

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;).

dmckee
can I say that; { if(s[i]=='-') if(i==0 || s[i-1]==',') {char *p=malloc(sizeof(char)*3);,in second example, char *p .. will be created only if two if statement are satisfied
gcc
You can, be aware however, that variable created in a scope go *out of scope* when you reach the matching `}`, and accessing them after that is an error.
dmckee
A: 

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.

Michael Aaron Safyan
A: 

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 begging 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.

shuttle87