// Join items on output stream. (1.00)
#include <stdio.h>
void join(const void *base, size_t nitems, size_t itemsize,
void (*putitem)(const void *item, FILE *stream),
const char* separator, FILE *stream)
{
const char *first = base, *last = first + nitems*itemsize;
if (first != last) {
for (;;) {
putitem(first, stream);
first += itemsize;
if (first == last) break;
fputs(separator
, stream
); }
}
}
void put_int(const void *item, FILE *stream)
{
fprintf(stream
, "%d", *(const int *) item
); }
void print_ints(const int *a, size_t n, int end)
{
join(a, n, sizeof *a, put_int, ", ", stdout);
}
int main(void)
{
int a[] = {1, 2, 3, 4};
print_ints(a, sizeof a / sizeof *a, '\n');
return 0;
}
Ly8gSm9pbiBpdGVtcyBvbiBvdXRwdXQgc3RyZWFtLiAoMS4wMCkKCiNpbmNsdWRlIDxzdGRpby5oPgoKdm9pZCBqb2luKGNvbnN0IHZvaWQgKmJhc2UsIHNpemVfdCBuaXRlbXMsIHNpemVfdCBpdGVtc2l6ZSwKICAgICAgICAgIHZvaWQgKCpwdXRpdGVtKShjb25zdCB2b2lkICppdGVtLCBGSUxFICpzdHJlYW0pLAogICAgICAgICAgY29uc3QgY2hhciogc2VwYXJhdG9yLCBGSUxFICpzdHJlYW0pCnsKICAgIGNvbnN0IGNoYXIgKmZpcnN0ID0gYmFzZSwgKmxhc3QgPSBmaXJzdCArIG5pdGVtcyppdGVtc2l6ZTsKICAgIGlmIChmaXJzdCAhPSBsYXN0KSB7CiAgICAgICAgZm9yICg7OykgewogICAgICAgICAgICBwdXRpdGVtKGZpcnN0LCBzdHJlYW0pOwogICAgICAgICAgICBmaXJzdCArPSBpdGVtc2l6ZTsKICAgICAgICAgICAgaWYgKGZpcnN0ID09IGxhc3QpIGJyZWFrOwogICAgICAgICAgICBmcHV0cyhzZXBhcmF0b3IsIHN0cmVhbSk7CiAgICAgICAgfQogICAgfQp9Cgp2b2lkIHB1dF9pbnQoY29uc3Qgdm9pZCAqaXRlbSwgRklMRSAqc3RyZWFtKQp7CiAgICBmcHJpbnRmKHN0cmVhbSwgIiVk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