Re: I think I'm gonna cry. (or newby problems with simple string function)
- From: "tedu" <tu@xxxxxxxxxxxxx>
- Date: 29 Sep 2005 16:13:44 -0700
robbie.carlton@xxxxxxxxx wrote:
> char* extract(char* str, int len) {
> char* out = (char*)malloc(len + 1);
> out = memcpy(out, str, len);
> out[len] = '\0';
> return out;
> }
> char** explode(char* str) {
> int nt = counttokens(str);
> if(!nt) {
> return 0;
> }
>
> char** ret = (char**)malloc(nt);
>
> int i = 0;
> int len = strlen(str);
> char ch;
> int start = 0;
> int mode = 0;
> int t = 0;
> for (i = 0; i < len; i++) {
> ch = str[i];
> if (mode == 0) {
> if(!isspace(ch)) {
> mode = 1;
> start = 0;
> }
> } else {
> if(istax(ch)) {
> ret[t] = extract(str + start, (i + 1) - start);
> t++;
> } else if(isspace(ch)) {
> mode = 0;
> ret[t] = extract(str + start, (i + 1) - start);
> t++;
> }
> }
> }
you aren't reassigning start nor using t in a meaningful way. pick
one. :)
.
- Follow-Ups:
- Prev by Date: Re: Do I need to free all memory that is malloced?
- Next by Date: Re: RAND between 0 and 1
- Previous by thread: trying to compile with clock_settime(...)
- Next by thread: Re: I think I'm gonna cry. (or newby problems with simple string function)
- Index(es):
Relevant Pages
|