I want to create the following looking 2D array of "-"
I attempted something like this but it did not work (for 10 by 10)
char table[10][10];
for (int i = 0; i < 10; i ){
for (int j = 0; j < 10; j )
{
strcpy(table[10][i], "-");
}
}
CodePudding user response:
The whole 2D array?
If not strings, use memset(table, '-', sizeof table);
to fill every byte with '-'
. No for
loop needed.
CodePudding user response:
char table[10][10];
for (size_t i = 0; i < sizeof(table) / sizeof(table[0]); i ){
for (size_t j = 0; j < sizeof(table[i]); j )
{
table[i][j] = `-`);
}
}
or memset(table, '-', sizeof(table))
If you want to have 10 strings (null character terminated)
for (size_t i = 0; i < sizeof(table) / sizeof(table[0]); i ){
memset(table[i], `-`, sizeof(table[i]) - 1);
table[i][sizeof(table[i]) - 1] = 0;
}
CodePudding user response:
Not advocating this for portability, but if you're using GCC, you can initialize at the declaration using the following GNU extension
char table[10][10] = { [0 ... 9] = {[0 ... 9] = '-'} };
(blatant ripoff of this answer)