direct string manuplation not working

S

shyam

Hi All

Here is a program which basically tokenizes a string based on space
seperation.
But it does not run bcoz i am directly trying to manuplate the string.
I cannot use standard string functions due to performance issues

can anybody figure out whats gone wrong with this



#include<stdio.h>
#include<string.h>
#include<malloc.h>


#define SP ' '


char **tokenize(char *);


char ** tokenize(char *ch)// to tokenize the string based on space
{


int count = 0;
char *mrk = ch;


while(*ch != '\0')
{
if(*ch == SP)


count++;


ch++;


}


count = count + 2;


ch = mrk;


char **c = (char **)malloc(count * sizeof(ch));


int count2;
char *fst,*lst;


fst = lst = ch;


for (count2 = 0; count2 < count - 1; count2++)
{


lst = strchr(lst,SP);


*lst = '\0'; //the DDD crashes at this point


lst++;


c[count2] = fst;


fst = lst;
}


c[count - 1] = NULL;


return c;



}


int main()
{

char *s = "hello how do u do";


char **t = tokenize(s);


printf("the str is %s\n",*t[0]);//similarly for s[1] & s[2]



}
 
A

Artie Gold

shyam said:
Hi All

Here is a program which basically tokenizes a string based on space
seperation.
But it does not run bcoz i am directly trying to manuplate the string.
I cannot use standard string functions due to performance issues

can anybody figure out whats gone wrong with this



#include<stdio.h>
#include<string.h>
#include<malloc.h>
Non-standard header. malloc() is declared in:
#include said:
#define SP ' '


char **tokenize(char *);


char ** tokenize(char *ch)// to tokenize the string based on space
{


int count = 0;
char *mrk = ch;


while(*ch != '\0')
{
if(*ch == SP)


count++;


ch++;


}


count = count + 2;


ch = mrk;


char **c = (char **)malloc(count * sizeof(ch));
There's no need to cast the return value of malloc(), doing so can mask
errors. Better:
char **c = malloc(count * sizeof *c);
int count2;
char *fst,*lst;


fst = lst = ch;


for (count2 = 0; count2 < count - 1; count2++)
{


lst = strchr(lst,SP);


*lst = '\0'; //the DDD crashes at this point
Right. `lst' points somewhere into the array of chars passed to the
function. In this case (see main() below) it is a string literal which
is not guaranteed to be modifiable.
lst++;


c[count2] = fst;


fst = lst;
}


c[count - 1] = NULL;


return c;



}


int main()
{

char *s = "hello how do u do"
Try:
char s[] = "hello how do u do";

[BTW, you had a missing semicolon above. Cut and paste *real* code in
the future.]
;
char **t = tokenize(s);


printf("the str is %s\n",*t[0]);//similarly for s[1] & s[2]



}
Next time:
1) Post *real* code.
2) Get rid of all those absolutely useless blank lines.

Also, be aware that there may be (and most likely are) further bugs in
your code; there's just no way I'm going to go through it with a fine
toothed comb when presented like this.

HTH,
--ag
 
B

Barry Schwarz

Hi All

Here is a program which basically tokenizes a string based on space
seperation.
But it does not run bcoz i am directly trying to manuplate the string.
I cannot use standard string functions due to performance issues

can anybody figure out whats gone wrong with this



#include<stdio.h>
#include<string.h>
#include<malloc.h>

Non-standard header. malloc and family are in stdio.h.
#define SP ' '


char **tokenize(char *);


char ** tokenize(char *ch)// to tokenize the string based on space

Why are you triple spacing between every line? Does your monitor
really show 300 lines or is there some reason you don't like to see as
much of your code as possible? However, you do get points for
indenting nicely.
{
int count = 0;
char *mrk = ch;
while(*ch != '\0')
{
if(*ch == SP)
count++;
ch++;
}
count = count + 2;
ch = mrk;
char **c = (char **)malloc(count * sizeof(ch));

Many pre-C99 systems do not tolerate definitions after executable
code.

Don't cast the return from malloc. It seldom helps and cause the
compiler to suppress a warning you really want to see.
int count2;
char *fst,*lst;
fst = lst = ch;
for (count2 = 0; count2 < count - 1; count2++)
{
lst = strchr(lst,SP);
*lst = '\0'; //the DDD crashes at this point

If you look in main, you will see that the argument you passed to this
function is a pointer to a string literal. This statement attempts to
change the contents of that literal. This is undefined behavior. An
immediate crash is one of the best types of undefined behavior.
lst++;
c[count2] = fst;
fst = lst;
}
c[count - 1] = NULL;
return c;
}

int main()
{
char *s = "hello how do u do";
char **t = tokenize(s);
printf("the str is %s\n",*t[0]);//similarly for s[1] & s[2]

The %s requires a char* for its argument. t is a char**; t[0] is a
char*; *t[0] is a char. I think you wanted t[0].

Don't you think your should print t[1], t[2], until you get to the
t[n] which is NULL.


<<Remove the del for email>>
 
J

Jirka Klaue

Default User:
Jordan Abel:


The standard header, stdio.h, as he said. What's confusing you?

Only if merely the first three letters are significant. :)

Jirka
 
F

Flash Gordon

Default said:
The standard header, stdio.h, as he said. What's confusing you?

The fact that malloc and friends are declared in stdlib.h and not
stdio.h possibly?
 
J

Jordan Abel

The standard header, stdio.h, as he said. What's confusing you?

the fact that thy're actually in stdlib.h, and, on my system at least,
including stdio.h does not cause declarations from stdlib.h to become
available.
 
D

Default User

the fact that thy're actually in stdlib.h, and, on my system at least,
including stdio.h does not cause declarations from stdlib.h to become
available.

Damn it. Sorry.


Brian
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top