Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > direct string manuplation not working

Reply
Thread Tools

direct string manuplation not working

 
 
shyam
Guest
Posts: n/a
 
      11-29-2005
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]



}

 
Reply With Quote
 
 
 
 
Artie Gold
Guest
Posts: n/a
 
      11-29-2005
shyam wrote:
> 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 <stdlib.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));

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

--
Artie Gold -- Austin, Texas
http://goldsays.blogspot.com (new post 8/5)
http://www.cafepress.com/goldsays
"If you have nothing to hide, you're not trying!"
 
Reply With Quote
 
 
 
 
Barry Schwarz
Guest
Posts: n/a
 
      11-29-2005
On 28 Nov 2005 18:02:46 -0800, "shyam" <> wrote:

>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>>
 
Reply With Quote
 
Jordan Abel
Guest
Posts: n/a
 
      11-29-2005
On 2005-11-29, Barry Schwarz <> wrote:
> Non-standard header. malloc and family are in stdio.h.


they're in what, now?
 
Reply With Quote
 
Default User
Guest
Posts: n/a
 
      11-29-2005
Jordan Abel wrote:

> On 2005-11-29, Barry Schwarz <> wrote:
> > Non-standard header. malloc and family are in stdio.h.

>
> they're in what, now?


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



Brian

 
Reply With Quote
 
Jirka Klaue
Guest
Posts: n/a
 
      11-29-2005
Default User:
> Jordan Abel:
>
>> On 2005-11-29, Barry Schwarz <> wrote:
>> > Non-standard header. malloc and family are in stdio.h.

>>
>> they're in what, now?

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


Only if merely the first three letters are significant.

Jirka
 
Reply With Quote
 
Martin Ambuhl
Guest
Posts: n/a
 
      11-29-2005
Default User wrote:
> Jordan Abel wrote:
>>On 2005-11-29, Barry Schwarz <> wrote:


>>>Non-standard header. malloc and family are in stdio.h.


>>they're in what, now?


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



Are you sure about that, Brian? What's <stdlib.h> for, then?
 
Reply With Quote
 
Flash Gordon
Guest
Posts: n/a
 
      11-29-2005
Default User wrote:
> Jordan Abel wrote:
>
>> On 2005-11-29, Barry Schwarz <> wrote:
>>> Non-standard header. malloc and family are in stdio.h.

>> they're in what, now?

>
> 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?
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.
 
Reply With Quote
 
Jordan Abel
Guest
Posts: n/a
 
      11-29-2005
On 2005-11-29, Default User <> wrote:
> Jordan Abel wrote:
>
>> On 2005-11-29, Barry Schwarz <> wrote:
>> > Non-standard header. malloc and family are in stdio.h.

>>
>> they're in what, now?

>
> 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.
 
Reply With Quote
 
Default User
Guest
Posts: n/a
 
      11-29-2005
Jordan Abel wrote:

> On 2005-11-29, Default User <> wrote:
> > Jordan Abel wrote:


> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
ElementTree.XML(string XML) and ElementTree.fromstring(string XML)not working Kee Nethery Python 12 06-27-2009 06:06 AM
bit fileds or bit manuplation surendra C Programming 2 06-23-2006 11:09 AM
__doPostBack and Response.direct not working in non-framed pages. =?Utf-8?B?Q2xhcw==?= ASP .Net 0 11-10-2005 01:35 PM
bit manuplation with java ali4ever4@gmail.com Java 4 12-19-2004 02:38 PM
bit manuplation ali4ever4@gmail.com Java 3 12-18-2004 03:39 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57