Jwoo
Jwoo

Reputation: 13

Why does changing array elements in a loop delete the array

So i was running some tests and there was strange behavior going on whenever I tried to change a character from a to 0. Instead of changing the element in the array like I expected, the entire array was deleted. Here is what I used to debug

#include <stdio.h>

int main()
{
    char test[] = "aa432aa";
    for(int i = 0; i<7; i++){
        printf("%s\n", test);
        if(test[i] == 'a'){
            test[i] = 0;
            printf("an a\n");
        }
        else{
            printf("not a\n");
        }
    }
    printf("%s", test);
}

And here is the output:

aa432aa                                                                                    
an a                                                                                       
                                                                                           
an a                                                                                       
                                                                                           
not a                                                                                      
                                                                                           
not a                                                                                      
                                                                                           
not a                                                                                      
                                                                                           
an a                                                                                       
                                                                                           
an a                                                                                       
       

Upvotes: 0

Views: 70

Answers (2)

biqarboy
biqarboy

Reputation: 852

This is because ASCII value 0 is actually equal to \0 and \0 is indicating the end of string in C. In the for loop when you set test[i] = 0;, this means it will convert the value of the test variable into "\0a432aa". So when you are trying to print it, it found an ending character at the beginning and do not able to print anything. To clear the idea, try to print a string like "xx\0a432aa", and it will only print "xx".

I think you are trying to put '0' instead of 'a'. If that is the case, your code should be like this:

#include <stdio.h>

int main()
{
    printf("\'\\0\' : %d\n", '\0');
    printf("0 : %d\n", 0);
    
    char t[] = "xx\0a432aa";
    printf("%s\n", t);

    char test[] = "aa432aa";
    for(int i = 0; i<7; i++){
        printf("%s\n", test);
        if(test[i] == 'a'){
            test[i] = '0';
            printf("an a\n");
        }
        else{
            printf("not a\n");
        }
    }
    printf("%s", test);
}

I also add 4 initial lines to give you intuition about the problem you are facing here. For further understanding you can check this post from stackoverflow.

Upvotes: 0

Bernardo Duarte
Bernardo Duarte

Reputation: 4264

As pointed out in the comments you should use test[i] = '0' not test[i] = 0, because '0' is the 0 character, while 0 is equal to '\0'. When printing, '\0' is treated as the final character of the string, so that's why, after changing the first 'a' to 0('\0'), you're not able to print it correctly.

Code:

#include <stdio.h>

int main()
{
    char test[] = "aa432aa";
    for(int i = 0; i<7; i++){
        printf("%s\n", test);
        if(test[i] == 'a'){
            test[i] = '0'; // Here!
            printf("an a\n");
        }
        else{
            printf("not a\n");
        }
    }
    printf("%s", test);
}

Output:

aa432aa
an a
0a432aa
an a
00432aa
not a
00432aa
not a
00432aa
not a
00432aa
an a
004320a
an a
0043200

Upvotes: 2

Related Questions