B
ben
why is it, in the below code, when there's a printf statement (the one
commented with /* ****** */) the final for loop prints out fine, but
without the commented with stars printf statement included in the code
there's a bus error on the fourth element in the final for loop?
final for loop print out when the /* ****** */ printf line is included
in the code:
ab
ba
bc
cb
ca
ac
final for loop print without that printf line included in the code:
ab
ba
bc
Bus error
any ideas what's wrong? and why would the inclusion of a printf line
make a difference to whether it works or not? seems strange to me.
thanks very much, ben.
#include <stdio.h>
#include <stdlib.h> /* malloc */
#include <string.h> /* strcmp */
int
main(void)
{
char *data[] = { "ab", "ba", "ab", "bc", "cb", "ba", "ab", "ba",
"cb", "bc", "ca", "ac", NULL };
char **udp; /* for list of pointers to unique strings */
unsigned number = 0; /* number of strings */
unsigned unique = 0; /* number of unique strings */
unsigned dpi, udpi; /* indexes */
unsigned char flag;
for( dpi = 0; data[dpi] != 0; dpi++ ) /* get number of strings */
number++;
udp = (char**)malloc(number); /* for list of pointers to unique
strings */
for( dpi = 0; dpi < number; dpi++ ) { /* loop per string in input
data */
flag = 0;
for( udpi = 0; udpi < unique && flag != 1; udpi++ ) { /* loop per
unique string collected so far */
printf("%s %s\n", data[dpi], udp[udpi]); /* ****** */
if( strcmp(data[dpi], udp[udpi]) == 0 )
flag = 1;
}
if( flag == 0 ) /* if unique store it in unique list */
udp[unique++] = data[dpi];
}
putchar('\n');
for( dpi = 0; dpi < unique; dpi++ ) /* print the list of unique
strings */
printf("%s\n", udp[dpi]);
return 0;
}
commented with /* ****** */) the final for loop prints out fine, but
without the commented with stars printf statement included in the code
there's a bus error on the fourth element in the final for loop?
final for loop print out when the /* ****** */ printf line is included
in the code:
ab
ba
bc
cb
ca
ac
final for loop print without that printf line included in the code:
ab
ba
bc
Bus error
any ideas what's wrong? and why would the inclusion of a printf line
make a difference to whether it works or not? seems strange to me.
thanks very much, ben.
#include <stdio.h>
#include <stdlib.h> /* malloc */
#include <string.h> /* strcmp */
int
main(void)
{
char *data[] = { "ab", "ba", "ab", "bc", "cb", "ba", "ab", "ba",
"cb", "bc", "ca", "ac", NULL };
char **udp; /* for list of pointers to unique strings */
unsigned number = 0; /* number of strings */
unsigned unique = 0; /* number of unique strings */
unsigned dpi, udpi; /* indexes */
unsigned char flag;
for( dpi = 0; data[dpi] != 0; dpi++ ) /* get number of strings */
number++;
udp = (char**)malloc(number); /* for list of pointers to unique
strings */
for( dpi = 0; dpi < number; dpi++ ) { /* loop per string in input
data */
flag = 0;
for( udpi = 0; udpi < unique && flag != 1; udpi++ ) { /* loop per
unique string collected so far */
printf("%s %s\n", data[dpi], udp[udpi]); /* ****** */
if( strcmp(data[dpi], udp[udpi]) == 0 )
flag = 1;
}
if( flag == 0 ) /* if unique store it in unique list */
udp[unique++] = data[dpi];
}
putchar('\n');
for( dpi = 0; dpi < unique; dpi++ ) /* print the list of unique
strings */
printf("%s\n", udp[dpi]);
return 0;
}