标签只能用作语句的一部分 错误 [英] Label can only be used as part of a statement Error

查看:41
本文介绍了标签只能用作语句的一部分 错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在浏览论坛,但我没有找到适用于我情况的这个问题的答案.我正在尝试使用'sort'(unix)进行系统调用,但是,我收到一条错误消息,标签只能是语句的一部分,而声明不是语句."这是导致错误的代码.

I have been looking through the forums but I have not found an answer to this question that applies to my situation. I am trying to make a system call to using 'sort' (unix), however, I receive an error saying, "a label can only be part of a statement and a declaration is not a statement." Here is the code causing the error.

int processid;  
switch(processid = fork()){                 //establishing switch statement for forking of processes.
case -1:
    perror("fork()");
    exit(EXIT_FAILURE);
    break;
case 0:
    char *const parmList[] = {"usr/bin/sort","output.txt","-o","output.txt",NULL};  //execv call to sort file for names.
    break;
default:
    sleep(1);
    printf("
Child process has finished.");
}

在系统调用中,我尝试按字母顺序对文件进行排序,以便按名称简单地收集类似的术语.

In the system call I am trying to sort a file in alphabetical order to simply gather like terms by name.

我傻眼了,因为这个错误发生在一个 char * const 中,其中包含我的 execv 系统调用的命令.此EXACT switch 语句适用于不同的程序文件.有人能发现我错过了什么吗?谢谢

I am so dumbfounded as the error for this occurs at a char * const of which contains the commands for my execv system call. This EXACT switch statement works on a different program file. Can someone spot what I am missing? Thanks

推荐答案

在 C(与 C++ 相反)中,声明不是语句.标签只能在语句之前.例如,您可以编写在标签后插入空语句

In C (opposite to C++) declarations are not statements. Labels may precede only statements. You can write for example inserting a null statement after the label

case 0:
    ;
    char *const parmList[] = {"usr/bin/sort","output.txt","-o","output.txt",NULL};  //execv call to sort file for names.
    break;

或者你可以用大括号将代码括起来

Or you can enclose the code in braces

case 0:
    {
    char *const parmList[] = {"usr/bin/sort","output.txt","-o","output.txt",NULL};  //execv call to sort file for names.
    break;
    }

请注意,在第一种情况下,变量的范围是 switch 语句,而在第二种情况下,变量的范围是标签下的内部代码块.该变量具有自动存储持续时间.所以退出对应的代码块后就不会活着了.

Take into account that in the first case the scope of the variable is the switch statement while in the second case the scope of the variable is the inner code block under the label. The variable has automatic storage duration. So it will not be alive after exiting the corresponding code blocks.

这篇关于标签只能用作语句的一部分 错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆