0

I am using fgets() for this. However, it seems that there are leftovers at the current loop, from the previous loop. But I declare input inside the loop, so isn't that a new array at every loop?

  int nn=0;
  while (1) {
    printf("%d\n", nn);
    if(++nn==3)break;
    char input[101];
    char* pch;
    printf("|%s|\n", input);
    fgets(input, 101, stdin);
    input[strlen(input) - 1] = '\0';
    printf("|%s|\n", input);
    pch = strtok (input," ");

    if(!strcmp(pch, "l")) {
      pch = strtok (NULL, " ");
      readFile(pch, rec, N, buckets, towns);
    }
    else if(!strcmp(pch, "e")) {
      printf("Exiting...\n");
      return;
    }
    else {
      printf("Unknown command. Exiting...\n");
      return;
    }
  }

Output:

0
||
l t200.bin
|l t200.bin|
Records found in file 200 
1
|l|
l t200.bin
|l t200.bin|
Records found in file 200 
2

I am worried that the new command to serve will be tangled up with the previous one, especially if leftovers are kept in input.

gsamaras
  • 71,951
  • 46
  • 188
  • 305
  • You print `input` before it is initialised; it only has meaningful contents after you've called `fgets` unless that returns `NULL` (which you don't check). – M Oehm Oct 18 '14 at 17:58
  • `char input[101];` -> `char input[101]="";` – BLUEPIXY Oct 18 '14 at 17:59
  • "isn't that a new array at every loop?" -- Only conceptually; in practice, in most implementations, it's the same memory. – Jim Balter Oct 18 '14 at 18:00
  • The input is shown at 2nd piece of code. I will check your comments later, because now I can't. – gsamaras Oct 18 '14 at 18:01
  • That's why one should initialize the variables before using it. – ani627 Oct 18 '14 at 18:06
  • @1336087 input *is* initialized, by the fgets. The OP is pointlessly printing it out before reading as well as after ... see my answer. – Jim Balter Oct 18 '14 at 18:10
  • @JimBalter: Why to print value of a variable before assigning any values to it? OP is printing values without initializing so it may output anything! – ani627 Oct 18 '14 at 18:13
  • 2
    @JimBalter I think the "point" of doing the print is the curiosity that motivates the question (about why the old content is kept, and what implications this has, if one should be worried and what one should worry about, etc). – HostileFork says dont trust SE Oct 18 '14 at 18:28
  • @HostileFork Perhaps, and that's a good insight, but the OP offered no indication that that is why it's there; it's normal to put a comment on code that's only there for investigation. Note that all these comments about initializing input before using it are nonsense ... it's the bogus use that's the problem, not the initialization, which is properly done by the fgets. – Jim Balter Oct 18 '14 at 18:44

2 Answers2

1

However, it seems that there are leftovers at the current loop, from the previous loop. But I declare input inside the loop, so isn't that a new array at every loop?

Each time you enter the scope in the loop, for each iteration, you have an array with undefined content. That is how local arrays declared in functions work. So your first pass through the loop could (theoretically) have input containing anything. In your case it happened to be an effectively empty string...but that wasn't required. Subsequent iterations happened to have the previous content...but again not required, it could contain anything.

Note that if an array is declared globally in C then the elements will be set to the default values:

Why are global and static variables initialized to their default values?

Though you are effectively "getting a 'new' (uninitialized) array every loop", it's important to realize you are also "losing the old array every loop". While it is likely in this case that the array will be getting the same memory address on each iteration, that is not a guarantee. You could not save the address of the array in a pointer declared outside the loop and be assured it would be the same on subsequent iterations.

Community
  • 1
  • 1
-1

You have two instances of

printf("|%s|\n", input);

one before you read into input and one after; just remove the first one, which is accessing input before it is set, resulting in undefined behavior.

isn't that a new array at every loop?

Only conceptually; in practice, in most implementations, it's the same memory (which is allocated on the stack). That's why you're seeing the results from the previous iteration. On the first iteration you're printing whatever junk happens to be on the stack (which is probably NULs). With an implementation that really allocated a new array on every iteration, you would be printing junk on every iteration ... or your program could crash; again, it's undefined behavior.

Also, you should check if fgets returns NULL -- which indicates EOF or error -- and terminate your loop if so, not just if you read an 'e'. The contents of input after fgets returns NULL is indeterminate.

Jim Balter
  • 16,163
  • 3
  • 43
  • 66