Error "a label can only be part of a statement and a declaration is not a statement" solution

Original: https://blog.csdn.net/weiyuefei/article/details/31775043 

 When I wrote the code before, I used a goto statement, and the compilation prompts a label can only be part of a statement and a declaration is not a statement. I didn’t understand it at first, because I haven’t encountered a similar error before, googled it. I couldn’t find a satisfactory answer. Finally, I carefully pondered the error and compared the code. Finally, I found the crux of the problem: it turned out to be the error caused by the variable declaration after the label. The label can only be a statement. The declaration of a variable is not a statement.

        The rough flow of the original code is as follows:

        if (!zmhss_fsready()) {
            goto defaul_init;
        }

        ........

defaul_init:

        struct rlimit     rlmt;

        if (ccf-> rlimit_nofile! = -1) {             rlmt.rlim_cur = (rlim_t) ccf-> rlimit_nofile;             rlmt.rlim_max = (rlim_t) ccf-> rlimit_nofile;

            if (setrlimit(RLIMIT_NOFILE, &rlmt) == -1) {

                   exit(2);
            }
       }

 

        It is the above sentence of struct rlimit rlmt; that finally led to the error message in the compilation process. The solution is to move this sentence of struct rlimit rlmt; to the variable declaration before the label, that is, the modified code structure is as follows:

        struct rlimit     rlmt;

        if (!zmhss_fsready()) {
            goto defaul_init;
        }

        ........

defaul_init:

        if (ccf-> rlimit_nofile! = -1) {             rlmt.rlim_cur = (rlim_t) ccf-> rlimit_nofile;             rlmt.rlim_max = (rlim_t) ccf-> rlimit_nofile;

            if (setrlimit(RLIMIT_NOFILE, &rlmt) == -1) {

                   exit(2);
            }
       }

        After the above modification, the problem was solved. An extension can be made from this problem, that is, when writing code, variable declarations should not appear after the label. For example, the case structure in the switch statement may also encounter similar problems.

 

PS: From a #if...#endif macro block goto to outside the macro block, there will be a compilation warning.
 

Guess you like

Origin blog.csdn.net/xiaolei251990/article/details/85133585