Replace the first line with spaces

cmultidimensional-arrayreplace

I'm trying to replace the first line with spaces, what is it that is wrong here?

#include <stdio.h>
int main(void){
    char text[5][10]={
        {'a','a','a','a','a','a','a','a','a','\0'},
        {'a','a','a','a','a','a','a','a','a','\0'},
        {'a','a','a','a','a','a','a','a','a','\0'},
        {'a','a','a','a','a','a','a','a','a','\0'},
        {'a','a','a','a','a','a','a','a','a','\0'},
    };


    for (int i=0;i<10;i++){
        text[i]=' ';
    }

    for (int i=0;i<5;i++){
        printf("%s\n",text[i]);
    }

    return 0;
}

Best Answer

You have two problems.

  1. You are only supplying one dimension in your first loop. It should be:

    text[0][i]=' ';

  2. You only have 9 'a's, but you are replacing with 10 spaces, so you'll overwrite the null terminator of the first line. so I think you're whole loop should be:

for (int i=0;i<9;i++){
    text[0][i]=' ';
}