#include #include #include char **array; int wrap (char *msg); int main (void) { array = (char **) malloc (sizeof (char *)); int msgCount, i; char msg[256] = "USB DEVICE FOUND 007\nUSB DEVICE INIT ERROR\nCOOKIE JAR\n\0"; msgCount = wrap (msg); for (i = 0; i < msgCount; i++) { printf ("%s\n", array[i]); free (array[i]); } strcpy (msg, "KEN SENT ME\nBROWN PAPER BAG\n"); msgCount = wrap (msg); for (i = 0; i < msgCount; i++) { printf ("%s\n", array[i]); free (array[i]); } free (array); } int wrap (char *msg) { printf ("begin\n"); char *tmp = (char *) malloc (sizeof (char)); int len = strlen (msg), i, msgCount = 0, ret; for (i = 0; i < len; i++) { if (msg[i] == '\n') msgCount++; } array = (char **) realloc (array, msgCount * sizeof (char *)); for (i = 0; i < msgCount; i++) { len = strlen (msg); ret = (int) strchr (msg, '\n') - (int) msg; array[i] = (char *) malloc ((ret + 1) * sizeof (char)); strncpy (array[i], msg, ret); array[i][ret] = '\0'; tmp = (char *) realloc (tmp, (len - ret) * sizeof (char)); strncpy (tmp, msg + ret + 1, len - ret); strcpy (msg, tmp); } free (tmp); return msgCount; }