本文介绍了混合声明和codeS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我编译功能的GCC -o沙地-Wall -ansi -pedantic-错误dene.c
,海湾合作委员会发出任何错误。(你可以看看它与字符​​开始的行...,如果在循环)

 静态无效remove_negation(字符* S,字符* S1)
          {
             焦炭** cmainp =的malloc(sizeof的(字符*)* 1);
                        INT LEN = 0; INT D = 0; INT I = 0;
            cmainp [0] =的malloc(sizeof的(字符)* 300);
            LEN = strlen的(S);
           对于(i = 0; I< LEN ++ I)
             {如果(S [I] ==' - ')
               如果(ⅰ== 0 || S [I-1] ==',')
      / *看* / {字符* p =的malloc(sizeof的(字符)* 3); /*看*/                ++我; P [0] = S [I]; P [1] ='\\ 0';              的strcat(S1,,);的strcat(S1,P);自由(对);
               继续;
             }
            cmainp [0]并[d] = S [I];
               ++ D组;
               } cmainp [0] [D + 1] ='\\ 0';
             的strcpy(cmainp [0],S);
             免费(cmainp [0]);
              }

但是,上面的函数编译时被格式化用gcc
 ,海湾合作委员会发出的错误;

dene.c:10:错误:ISO C90不允许混合使用声明和code

 静态无效remove_negation(字符* S,字符* S1)
          {
             焦炭** cmainp =的malloc(sizeof的(字符*)* 1);
          / * *外观/ cmainp [0] =的malloc(sizeof的(字符)* 300); /*看*/
                        INT LEN = 0; INT D = 0; INT I = 0;            LEN = strlen的(S);
           对于(i = 0; I< LEN ++ I)
             {如果(S [I] ==' - ')
               如果(ⅰ== 0 || S [I-1] ==',')
        {字符* p =的malloc(sizeof的(字符)* 3);                ++我; P [0] = S [I]; P [1] ='\\ 0';              的strcat(S1,,);的strcat(S1,P);自由(对);
               继续;
             }
            cmainp [0]并[d] = S [I];
               ++ D组;
               } cmainp [0] [D + 1] ='\\ 0';
             的strcpy(cmainp [0],S);
             免费(cmainp [0]);
              }

和最后一个,以下错误的gcc发出

dene.c:16:错误:预期前pression'字符

dene.c:20:错误:'P1'未申报(在一次使用此功能)

dene.c:20:错误:(每个未声明的标识符报道只有一次

dene.c:20:错误:它出现在每个功能)

 静态无效remove_negation(字符* S,字符* S1)
          {
             焦炭** cmainp =的malloc(sizeof的(字符*)* 1);                        INT LEN = 0; INT D = 0; INT I = 0;
             cmainp [0] =的malloc(sizeof的(字符)* 300);
            LEN = strlen的(S);
           对于(i = 0; I< LEN ++ I)
             {如果(S [I] ==' - ')
        / * *看/的char * p = malloc的(sizeof的(字符)* 3); /*看*/
               如果(ⅰ== 0 || S [I-1] ==',')
              {                ++我; P [0] = S [I]; P [1] ='\\ 0';              的strcat(S1,,);的strcat(S1,P);自由(对);
               继续;
             }
            cmainp [0]并[d] = S [I];
               ++ D组;
               } cmainp [0] [D + 1] ='\\ 0';
             的strcpy(cmainp [0],S);
             免费(cmainp [0]);
              }

问题是为什么它们之间存在的差异。


解决方案

在K&安培; R和ANSI C,必须始终把声明在范围块的开始。这一要求放松了C99。

那么,什么一个范围块?通过划定一个区域 {}

因此​​,在你上面的例子中声明

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

由于它之后立即发生是O​​K了 {,而

  {
  焦炭** cmainp =的malloc(sizeof的(字符*)* 1);  / * *外观/ cmainp [0] =的malloc(sizeof的(字符)* 300); /*看*/  INT LEN = 0; ...

由于assigment而来的 {和第二个声明( INT LEN = 0)之间发生故障/ p>

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.

解决方案

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

这篇关于混合声明和codeS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-15 05:22