sah@gnu.ai.mit.edu (Tarador) (04/30/91)
The following program is akin to the MUF primitive EXPLODE, if any of you are familiar with MUCK. Anyway, it works like this: x = explode(char *data, char *trigger, char **array); Where x is an int, *data is a a data string, *trigger is the string to explode on, and **array is filled by results. So, x = explode("Hello.Test", ".", results); That SHOULD put "Hello" in results[0] and "Test" in results[1]. But, for some reason, I keep getting a bus error. Program follows: int explode(); main() { int c,cc; char **arr, x[50], trig[5]; gets(x); gets(trig); cc = explode(x, trig, arr); for (c=0;c<cc;++c) printf("%s\n", arr[c]); } int explode(char *data, char *trigger, char **array) { int x; int last=0, ctr=0; char dummy[512]; for (x=0;x<strlen(data);++x) { if (!strncmp(&data[x], trigger, strlen(trigger))) { /* Here's the bus error*/ strncpy(array[ctr], &data[last], x); last = x+strlen(trigger); ++ ctr; } } return ctr; } Please post suggestions, etc., to sah@gnu.ai.mit.edu. Thanks. -- +-------------------------------------------------------------------------+ | Tarador Dranon MIT: sah@gnu.ai.mit.edu | | C-Guild: vidco%cguild%valnet@iuvax.cs.indiana.edu | +-------------------------------------------------------------------------+
halloran@sldv21.mdcbbs.com (05/03/91)
In article <15431@life.ai.mit.edu>, sah@gnu.ai.mit.edu (Tarador) writes: > > The following program is akin to the MUF primitive EXPLODE, if any of > you are familiar with MUCK. Anyway, it works like this: > x = explode(char *data, char *trigger, char **array); > Where x is an int, *data is a a data string, *trigger is the string to > explode on, and **array is filled by results. > So, x = explode("Hello.Test", ".", results); > That SHOULD put "Hello" in results[0] and "Test" in results[1]. But, > for some reason, I keep getting a bus error. Program follows: > > int explode(); > main() > { > int c,cc; > char **arr, x[50], trig[5]; > > gets(x); gets(trig); > cc = explode(x, trig, arr); > for (c=0;c<cc;++c) > printf("%s\n", arr[c]); > } > int explode(char *data, char *trigger, char **array) > { > int x; > int last=0, ctr=0; > char dummy[512]; > > for (x=0;x<strlen(data);++x) { > if (!strncmp(&data[x], trigger, strlen(trigger))) { > /* Here's the bus error*/ strncpy(array[ctr], &data[last], x); You have failed to allocate memory into which you wish to copy. array (or main::arr), is typed as a pointer to a pointer to char but where does it point??? Anywhere! You have not initialized the pointer to valid memory. > last = x+strlen(trigger); > ++ ctr; > } > } > return ctr; > } > > Please post suggestions, etc., to sah@gnu.ai.mit.edu. Thanks. > -- > +-------------------------------------------------------------------------+ > | Tarador Dranon MIT: sah@gnu.ai.mit.edu | > | C-Guild: vidco%cguild%valnet@iuvax.cs.indiana.edu | > +-------------------------------------------------------------------------+ -- Mark J. Halloran | Voice: (314) 233-6973 McDonnell Douglas M&E | Fax: (314) 232-8698 Dept C705 MC 3065386 | Internet: halloran@sldev1.mdcbbs.com St. Louis, Mo. 63166 | UUCP: uunet!sldev1.mdcbbs!halloran