mixed declarations and codes

后端 未结 1 1164
温柔的废话
温柔的废话 2021-01-12 02:41

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,)

         


        
1条回答
  •  耶瑟儿~
    2021-01-12 03:12

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

    0 讨论(0)
提交回复
热议问题