Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- static void print_array_char(char *, size_t);
- static void print_array_int(int *, size_t);
- static void print_array_unsigned(unsigned *, size_t);
- static void print_array_char(char *a, size_t n) {
- printf("[");
- for (size_t i = 0; i < n; ++i) {
- printf("%s%c", ((i == 0) ? "" : ", "), a[i]);
- }
- puts("]");
- }
- static void print_array_int(int *a, size_t n) {
- printf("[");
- for (size_t i = 0; i < n; ++i) {
- printf("%s%d", ((i == 0) ? "" : ", "), a[i]);
- }
- puts("]");
- }
- static void print_array_unsigned(unsigned *a, size_t n) {
- printf("[");
- for (size_t i = 0; i < n; ++i) {
- printf("%s%d", ((i == 0) ? "" : ", "), a[i]);
- }
- puts("]");
- }
- #define print_array(a, n) _Generic((a) \
- , char *: print_array_char \
- , int *: print_array_int \
- , unsigned *: print_array_unsigned \
- )(a, n)
- int main(void) {
- char array_char[] = { 'h', 'e', 'l', 'l', 'o' };
- int array_int[] = { -2, -1, 1, -2 };
- unsigned array_uint[] = { 1, 2, 3 };
- print_array(array_char, sizeof array_char / sizeof array_char[0]);
- print_array(array_int, sizeof array_int / sizeof array_int[0]);
- print_array(array_uint, sizeof array_uint / sizeof array_uint[0]);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement