In 'comp.lang.c',
(ben) wrote:
> udp = (char**)malloc(number); /* for list of pointers to unique
> strings */
This is the problem. You don't allocate enough memory. See the fixed code
hereby:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main (void)
{
char const *data[] =
{"ab", "ba", "ab", "bc", "cb", "ba", "ab", "ba",
"cb", "bc", "ca", "ac", NULL};
/* number of strings */
unsigned number = 0;
/* get number of strings */
{
unsigned dpi;
for (dpi = 0; data[dpi] != NULL; dpi++)
{
number++;
}
}
{
/* number of unique strings */
unsigned unique = 0;
/* for list of pointers to unique strings */
char const **udp = malloc (number * sizeof *udp);
if (udp != NULL)
{
unsigned dpi;
/* loop per string in input data */
for (dpi = 0; dpi < number; dpi++)
{
int already = 0;
unsigned udpi;
/* loop per unique string collected so far */
for (udpi = 0; udpi < unique && !already; udpi++)
{
printf ("%s %s\n", data[dpi], udp[udpi]);
already = strcmp (data[dpi], udp[udpi]) == 0;
}
/* if unique store it in unique list */
if (!already)
{
udp[unique++] = data[dpi];
}
}
}
putchar ('\n');
{
unsigned dpi;
/* print the list of unique strings */
for (dpi = 0; dpi < unique; dpi++)
{
printf ("%s\n", udp[dpi]);
}
}
free (udp), udp = NULL;
}
return 0;
}
Feel free to ask for details.
--
-ed- get my email here:
http://marreduspam.com/ad672570
The C-language FAQ:
http://www.eskimo.com/~scs/C-faq/top.html
C-reference:
http://www.dinkumware.com/manuals/reader.aspx?lib=c99
FAQ de f.c.l.c :
http://www.isty-info.uvsq.fr/~rumeau/fclc/