Thread: Char array in while scope does not do what I excpected.

  1. #1
    Registered User
    Join Date
    Dec 2017
    Posts
    22

    Char array in while scope does not do what I excpected.

    Code:
    while ( fgets(line, 100, file) !=NULL )
    {
    char path[100];
    int i =1;
    
    strcat(path, cwd);
    strcat(path, "/");
    strcat(path, line);
    printf("path is: %s", path);
    
            i++;
    
    printf("i is: %d\n", i);
    }
    Input File:
    Code:
    hello
    this
    is
    a
    test
    Output:

    Code:
    babaliaris@babaliaris-desktop:~/dev/programming/c/test$ ./Main 
    path is: /home/babaliaris/dev/programming/c/test/hello
    i is: 2
    path is: /home/babaliaris/dev/programming/c/test/hello
    /home/babaliaris/dev/programming/c/test/this
    i is: 2
    path is: /home/babaliaris/dev/programming/c/test/hello
    /home/babaliaris/dev/programming/c/test/this
    /home/babaliaris/dev/programming/c/test/is
    i is: 2
    path is: /home/babaliaris/dev/programming/c/test/hello
    /home/babaliaris/dev/programming/c/test/this
    /home/babaliaris/dev/programming/c/test/is
    /home/babaliaris/dev/programming/c/test/a
    i is: 2
    path is: /home/babaliaris/dev/programming/c/test/hello
    /home/babaliaris/dev/programming/c/test/this
    /home/babaliaris/dev/programming/c/test/is
    /home/babaliaris/dev/programming/c/test/a
    /home/babaliaris/dev/programming/c/test/testi is: 2
    *** stack smashing detected ***: <unknown> terminated
    Aborted (core dumped)
    Expected Output:
    Code:
    babaliaris@babaliaris-desktop:~/dev/programming/c/test$ ./Main 
    path is: /home/babaliaris/dev/programming/c/test/hello
    path is: /home/babaliaris/dev/programming/c/test/this
    path is: /home/babaliaris/dev/programming/c/test/is
    path is: /home/babaliaris/dev/programming/c/test/a
    path is: /home/babaliaris/dev/programming/c/test/testbabaliaris@babaliaris-desktop:~/dev/programming/c/test$
    The expected output works if i change the loop like this:
    Code:
    while ( fgets(line, 100, file) !=NULL )
    {
    
    char path[100];
    path[0] ='\0';
    
    strcat(path, cwd);
    strcat(path, "/");
    strcat(path, line);
    printf("path is: %s", path);
    
    }
    
    
    As you can see int i is recreated for each iteration but char path[100] is getting appended each iteration like it's a variable outside of the while loop scope.

    Why is that?
    Last edited by babaliaris; 12-01-2018 at 08:45 AM.

  2. #2
    Registered User
    Join Date
    Dec 2017
    Posts
    1,633
    You seem to be assuming that there will be some kind of automatic initialization of the char array. That is not the case. Although in some sense it is "recreated" for every iteration, it is almost certainly "recreated" in exactly the same space on the stack. So since you are not initializing it in any way, it will "contain" the last data that it had.

    You could try:
    Code:
    while ( fgets(line, 100, file) != NULL ) {
        char path[100] = {0};   // inits the entire array with 0's
        ...
    }
    Or better yet for your purposes.
    Code:
    while ( fgets(line, 100, file) != NULL ) {
        char path[100];
        strcpy(path, cwd);   // make the first one a strcpy, not strcat
        ...
    }
    BTW, your post is excellent except for the way you've posted the code. You need to make sure you copy/paste it as plain text so it's actually readable. Somehow you've included some of your own formatting. Also, it's always best to actually post a complete program so we can more easily run it.
    Last edited by john.c; 12-01-2018 at 09:14 AM.
    A little inaccuracy saves tons of explanation. - H.H. Munro

  3. #3
    Registered User
    Join Date
    Dec 2017
    Posts
    22
    Quote Originally Posted by john.c View Post
    You seem to be assuming that there will be some kind of automatic initialization of the char array. That is not the case. Although in some sense it is "recreated" for every iteration, it is almost certainly "recreated" in exactly the same space on the stack. So since you are not initializing it in any way, it will "contain" the last data that it had.

    You could try:
    Code:
    while ( fgets(line, 100, file) != NULL ) {
        char path[100] = {0};   // inits the entire array with 0's
        ...
    }
    Or better yet for your purposes.
    Code:
    while ( fgets(line, 100, file) != NULL ) {
        char path[100];
        strcpy(path, cwd);   // make the first one a strcpy, not strcat
        ...
    }
    BTW, your post is excellent except for the way you've posted the code. You need to make sure you copy/paste it as plain text so it's actually readable. Somehow you've included some of your own formatting. Also, it's always best to actually post a complete program so we can more easily run it.
    Thank you for your reply! I understand what you mean, lately I'm programming too much in c++ and i forgot that c is not initializing anything. About the code, yes I copied paste it from an Idle so this must be the problem. Next time I will have it in mind.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 09-25-2014, 06:12 AM
  2. Replies: 2
    Last Post: 09-25-2014, 04:03 AM
  3. how come I can exceed array scope
    By incorrect in forum C Programming
    Replies: 8
    Last Post: 05-11-2012, 05:30 AM
  4. how to access array outside its scope
    By shikhardeep in forum C++ Programming
    Replies: 1
    Last Post: 04-15-2012, 06:06 AM
  5. Replies: 3
    Last Post: 11-17-2008, 12:36 PM

Tags for this Thread