i did run it
whats the point of printing before the loop?
so
array[0] starts off as nothing, then strcat to 'a'
then with strcpy, array[1] becomes 'a', then strcat to 'aa', and so on
?
yup I get it entirely =]
LOL - and I was thinking "must start at i=1" while editing the code before posting it.
Oh well.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
WaterSerpentM:
The following is an alternative method for displaying what you wanted. It eliminates the need for both strcpy(), and strcat() functions!
Study it carefully.
Code:#include <stdio.h> // string.h no longer needed! int main(void) { // The line below will initialize the first array, ba[0] to "a" // and initialize all other bytes to Nul bytes ('\0') char ba[10][12] = {{"a"}}; int x = 0; printf("%s\n",ba[0]); // Print first array for(int i = 1;i < 10;i++) // For each of the remaining arrays { for(x = 0; x <= i; x++) // For each 'a' in the array { ba[i][x] = 'a'; } // The following line is actually redundent, as all the // other bytes were Nulled. But I would allways use it // to assure the string is Null terminated, in case the // ba array is reused later! ba[i][x] = '\0'; printf("%s\n",ba[i]); // Print next array } return 0; }
check this out =]
Code:#include <stdio.h> #include <string.h> int main() { char chara[10][10]; int i; for(i=0;i<10;i++){ strcpy(chara[i],chara[i-1]); strcat(chara[i],"a"); } for(i=9;i>-1;i--){ printf("%s\n",chara[i]); } return 0; }
The first time through the loop, i == 0, and i - 1 IS -1!!!Code:for(i=0;i<10;i++){ strcpy(chara[i],chara[i-1]); strcat(chara[i],"a"); }
I don't understand how this works! It is wrong and at some point it will fail!
Better:Code:for(i=9;i>-1;i--){ printf("%s\n",chara[i]); }
Code:for(i=9;i >= 0;i--){ printf("%s\n",chara[i]); }
it works, it outputs:
aaaaaaaaaa
aaaaaaaaa
aaaaaaaa
aaaaaaa
aaaaaa
aaaaa
aaaa
aaa
aa
a
I gave you two different solutions that work, but you keep going back to code that is wrong!
that was salem's coding originally,
and it looks more construct than yours