18

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("\nChild process has finished.");
}

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

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

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
BologneseBandit
  • 187
  • 1
  • 1
  • 6
  • 2
    `sort` is a normal program, not a system-call. And declarations is a `switch` statement without a block are a very bad idea anyway. A good C book whould make clear **why**. Sidenote: Any reason you make the array `const`, but not what they point to? You must not change string literals. And where do you execute that program? Provide a [mcve]. – too honest for this site Sep 21 '17 at 11:46
  • Which part of the question is Unix specific? If none, then please don't spam with unrelated tags. – Gerhardh Sep 21 '17 at 13:10
  • What is tripping you up about the error message? It is pretty clearly written. – Mad Physicist Sep 21 '17 at 14:42

2 Answers2

29

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;
    }

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.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
1

When define a variable below a label, you shall tell the scope of the variable(using brace).

int processid;
switch(processid = fork())
{                 //establishing switch statement for forking of processes.
    case -1:
        perror("fork()");
        exit(0);
        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("\nChild process has finished.");
}
Wayne Chen
  • 305
  • 2
  • 15