All pastes #248815 Raw Edit

Mine

public c v1 · immutable
#248815 ·published 2006-11-15 01:03 UTC
rendered paste body
#include <stdio.h>#include <stdlib.h>#include <string.h>char **array;intwrap (char *msg);intmain (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);}intwrap (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;}