On 29 Sep 2005 15:45:11 -0700,
robbie.carlton@gmail.com wrote in
comp.lang.c:
[color=blue]
> Hello!
>
> I've programmed in c a bit, but nothing very complicated. I've just
> come back to it after a long sojourn in the lands of functional
> programming and am completely stumped on a very simple function I'm
> trying to write. I'm writing a function that takes a string, and
> returns an array of strings which are the result of splitting the input
> on whitespace and parentheses (but the parentheses should also be
> included in the array as strings).
>
> an example:
>
> explode("foo bar baz") -> ["foo", "bar", "boys"]
> explode("foo(bar)baz") -> ["foo", "(", "bar", ")", "baz"][/color]
You haven't shown us any code that calls this function. Do you
actually call it with string literals, and does it attempt to modify
them? Modifying string literals is undefined behavior.
[color=blue]
> Now I thought I had it. But what I've got now causes a bus error. So
> I'm going to post all of the code (sorry) and maybe wiser minds than me
> can work it out. Please remember I'm a noob so I would prefer things as
> unobfuscated as possible, and I know how bad the style of my code is
> also, I'm trying to make it work first.
>
> Thanks in advance, here's the code:
>
> #include <stdio.h>[/color]
You haven't included <stdlib.h>, so you don't have a prototype for
malloc() in scope. Calling malloc() without a prototype produces
undefined behavior.
You haven't included <string.h>, so you don't have a prototype for
memcpy() in scope. Calling memcpy() without a prototype produces
undefined behavior.
You haven't included <ctype.h>, so you don't have a prototype for
isspace() in scope.
[color=blue]
> char* extract(char* str, int len) {[/color]
Since the sizeof operator yields a value of type size_t, and malloc()
accepts a single argument of type size_t, why are you using int? It
may not cause a problem in this case, but ultimately you are asking
for a signed/unsigned clash.
[color=blue]
> char* out = (char*)malloc(len + 1);[/color]
No, casting the value returned by malloc() is wrong. You probably did
this to shut up a compiler diagnostic, caused by your failure to
include <stdlib.h> and have a prototype in scope.
[color=blue]
> out = memcpy(out, str, len);
> out[len] = '\0';
> return out;
> }
>
> char istax(int ch) {
> int out = (ch=='(') | (ch==')');
> return out;
> }
>
> char** explode(char* str) {
> int nt = counttokens(str);
> if(!nt) {
> return 0;
> }
>
> char** ret = (char**)malloc(nt);[/color]
Whatever you are using, it is not a conforming C compiler, not
conforming to any version of the C language standard. Or you are not
using it that way.
Versions of the C standard prior to 1999 would not allow the
declaration above, because it comes after executable statements in the
current block. And versions of the C standard from and after 1999
will not allow a call to a function without at least a declaration in
scope, and you have none for malloc() or memcpy().
Examine your compiler's documentation to determine how to invoke it as
a conforming C compiler, or ask your question in a compiler-specific
group.
On the other hand, if you are compiling this code with a C++ compiler,
ask in comp.lang.c++.
[snip]
Fix the problems that I've pointed out and then, if you are compiling
this code with a conforming C compiler and still have problems, post
again.
--
Jack Klein
Home:
http://JK-Technology.Com
FAQs for
comp.lang.c
http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++
http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html