views:

165

answers:

8

Hello there, 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;
}
+1  A: 

I think you should adjust to

text[i][0] = ' ';
David Hedlund
First line... rather text[0][i] instead of text[i][0]
ammoQ
how 'bout text[0][i] = ' ';
Steve De Caux
sorry, cross post - I agree with ammoQ
Steve De Caux
oops, yes =) good catch
David Hedlund
+3  A: 

You're not indexing "across", you're indexing "down". Try:

text[0][i] = ' ';

to overwrite the first line's characters.

Also note that you have an Obi-wan; your loop overwrites the terminating character with a space as well.

unwind
Where's the character pointer? It's just a flat array of 50 elements. Should compile without problems.
Kosi2801
@Kosi2801: Oops, my mistake. Thanks, and fixed.
unwind
+3  A: 

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]=' ';
}
Phil Nash
Using a single dimenstion when working with multi-dimensional arrays is perfectly legal in C.The problem just lies in the second problem mentioned in the solution, where you try to replace 10 'a' and there are only 9 in each line of the array.
Kosi2801
+3  A: 

you will have to use

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

Now text will be

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'},
                                  };

Or

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

Now text will become

text[5][10]={
                                  {' ','a','a','a','a','a','a','a','a','\0'},
                                  {' ','a','a','a','a','a','a','a','a','\0'},
                                  {' ','a','a','a','a','a','a','a','a','\0'},
                                  {' ','a','a','a','a','a','a','a','a','\0'},
                                  {' ','a','a','a','a','a','a','a','a','\0'},
                                  };

Pick whichever solution you wanted. :)

Rakesh Juyal
It didn't pushed 'a' backward but it just changed the value in 0th index of all rows to ' '[space] :), So there is 1 space, 9 'a' and 1 '\0' in every row.
Rakesh Juyal
+1  A: 

I'd go one further - you're overwriting the null at the end of the first 'line'. Set your loop to iterate over 0..9:

for (int i=0;i<9;i++){
        text[0][i]=' ';
}
edoloughlin
ah cheers! i was wondering why it just pushed the a's backwards when it was (int i=0;i<10;i++)
sil3nt
+1  A: 
 for (int i=0;i<9;i++){
     text[0][i]=' ';
 }
jkndrkn
A: 
  • make your array length to 11 like: char text[5][10] to char text[5][11] and change the initialization accordingly.

  • Chanage your for loop, the condition 'i<10' to 'i<9'

The proble is with index counting.

shyam
A: 

Its double dimension array and not single dimension ans thats why this will not work.

Vivek
It's perfectly legal in C. char[5][10] is the same as char[50] and you're allowed to access the array in both ways.
Kosi2801
Then why its not working then ?
Vivek