#include #include #include int wrap (char *msg, char **array); int main (void) { char **array; 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, array); for (i = 0; i < msgCount; i++) printf ("%s\n", array[i]); // free (array); } int wrap (char *msg, char **array) { 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); }