Salem's (obviously untested) code started the loop at 0 instead of 1, an easy mistake to make from "muscle" memory. It was supposed to be something like this.
As an alternative to rstanley's last offering, instead of (implicitly) memsetting the array to 0's and adding the a's, you could memset it to a's and add the 0's! Overall I prefer rstanley's code, but there are many ways of doing it. Of course, you can obtain the output without the array at all, but I assume OP wants to fill the array for some unspecified (possibly unfathomable) reason.Code:#include <stdio.h> #include <string.h> #define SIZE 10 int main(void) { char a[SIZE][SIZE + 1]; // fill array strcpy(a[0], "a"); // set first string to "a" for (int i = 1; i < SIZE; i++) { // start at second string strcpy(a[i], a[i - 1]); // copy previous string to this string strcat(a[i], "a"); // postpend another a } // print array for (int i = 0; i < SIZE; ++i) printf("%s\n", a[i]); return 0; }
Code:#include <stdio.h> #include <string.h> #define SIZE 10 int main(void) { char a[SIZE][SIZE + 1]; // fill array with a's memset(a, 'a', sizeof a); // This adds a "diagonal" of 0's to the array of a's for (int i = 0; i < SIZE; ++i) a[i][i + 1] = '\0'; // print it for (int i = 0; i < SIZE; ++i) printf("%s\n", a[i]); return 0; } It fills the bytes of the array like this (0 means '\0') a0aaaaaaaaa aa0aaaaaaaa aaa0aaaaaaa aaaa0aaaaaa aaaaa0aaaaa aaaaaa0aaaa aaaaaaa0aaa aaaaaaaa0aa aaaaaaaaa0a aaaaaaaaaa0