0

I am trying to implement the code below, but it gives me errors here:

char **lines = tokenizer(buffer, "\n"); //use of undeclared identifier 'lines'
free(lines); //use of undeclared identifier 'lines'

Actual code piece:

int child = fork();
switch (child) {
    case 0 :
        // child process
        char **lines = tokenizer(buffer, "\n");
        free(lines);

How can I resolve this?

Michael Petrotta
  • 59,888
  • 27
  • 145
  • 179
Güngör Basa
  • 628
  • 2
  • 9
  • 27

2 Answers2

1

A declaration is not permitted immediately after a case statement. There are three possble fixes:

  • Enclose the code after the case in braces: case 0: { char **line = ...}.

  • Move the declaration to just after the switch:

     switch (child) {
         char **lines;
         case 0:
             lines = tokenizer(buffer, "\n");
    
  • Or, surprisingly, just add an extra ; after the case statement:

     switch (child) {
         case 0:
             ;
             char **lines = tokenizer(buffer, "\n");
    

See Why can't variables be declared in a switch statement?.

Community
  • 1
  • 1
Joseph Quinsey
  • 9,553
  • 10
  • 54
  • 77
1

Looks like you are compiling it with a C compiler but use C++ syntax.

Try this:

{
    char **lines = tokenizer(buffer, "\n");
    free(lines);
}

As a note - if you try to share variable and populate it from a child process - this will not work.

bbonev
  • 1,406
  • 2
  • 16
  • 33