>>101170134
>>101170039
Assuming you're not actually just setting the array to 9 where you could just use memset, you can honestly this kind of code can just be a macro.
#include <stdio.h>
// Add an argument for the length if you want to, to make it a bit more general
#define set_array(a) \
do { \
for (size_t i = 0; i < sizeof (a) / sizeof (a)[0]; ++i) { \
(a)[i] = i; \
} \
} while (0)
#define print_array(a, fmt) \
do { \
printf("{"fmt, (a)[0]); \
for (size_t i = 1; i < sizeof (a) / sizeof (a)[0]; ++i) { \
printf(", "fmt, (a)[i]); \
} \
printf("}\n"); \
} while (0)
int main()
{
int intarray[20];
char chararray[10];
float floatarray[5];
set_array(intarray);
set_array(chararray);
set_array(floatarray);
print_array(intarray, "%d");
print_array(chararray, "%hhd");
print_array(floatarray, "%.2f");
}
{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19}
{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
{0.00, 1.00, 2.00, 3.00, 4.00}
Using a void * and maybe a switch over a type is sometimes called for, but I wouldn't say this is one of these situations.