/** * A Little program to print the contents of an array * But this version has been expanded to be anything but * little. All of the expansions are in pursuit of genericness * of the the printing * * @author gtowell * Created: April 16, 2021 * Modified: April 21, 2021 * **/ #include #include #include #ifndef NULL #define NULL (void *)0 #endif /** * Print the contents of the structure into a string that is passed into * the function. This avoids awkward problems with free and malloc * @param item -- the thing to be printed * @param printInto -- the string to be modified * **/ void pprinter4(void * item, char* printInto) { char *citem = (char *)item; sprintf(printInto, "[%c]", *citem); } /** * print array of structs using the pprinter4 * @param base -- a pointer to the start of the array -- or at * least the start of the portion of the array to be printed * @param nmemb -- the number of item in the array (or at least * the number of items to be printed) * @param size -- the number of bytes of the items in the array * @param f - function pointer to a function of type pprinter4 * **/ void parray4(void * base, size_t nmemb, size_t size, void(*f)(void*, char*)) { char aa[100]; for (int i = 0; i < nmemb; i++) { f(base, aa); printf("%s ", aa); base += size; } printf("\n"); } /** * print structure into a string and return the string * @param -- the struct to be printed * @return -- a string rep of the struct * **/ char * pprinter3(void * item) { char *citem = (char *)item; char *rtn = malloc(6 * sizeof(char)); sprintf(rtn, "<%c>", *citem); return rtn; } /** * print array of structs using the pprinter3 * @param base -- a pointer to the start of the array -- or at * least the start of the portion of the array to be printed * @param nmemb -- the number of item in the array (or at least * the number of items to be printed) * @param size -- the number of bytes of the items in the array * @param f - function pointer to a function of type pprinter3 * **/ void parray3(void * base, size_t nmemb, size_t size, char*(*f)(void *)) { for (int i = 0; i < nmemb; i++) { char *tmp = f(base); printf("%s ", tmp); base += size; free(tmp); } printf("\n"); } /** * A ridiculously complex way to print an array, but almost * generic and, even better, it follows qsort * @param base -- a pointer to the start of the array -- or at * least the start of the portion of the array to be printed * @param nmemb -- the number of item in the array (or at least * the number of items to be printed) * @param size -- the number of bytes of the items in the array * @param fmt -- a formatting string * **/ void parray2(void * base, size_t nmemb, size_t size, char* fmt) { for (int i = 0; i < nmemb; i++) { char c = *((char *)base); printf(fmt, c); base += size; } printf("\n"); } int main(int argc, char const *argv[]) { srand(time(NULL)); char aa[100]; for (int i = 0; i < 100; i++) aa[i] = 'a' + (rand() % 26); parray2(aa, 100, sizeof(char), "%c"); parray3(aa, 10, sizeof(char), pprinter3); parray4(aa, 10, sizeof(char), pprinter4); }