direct string manuplation not working

Discussion in 'C Programming' started by shyam, Nov 29, 2005.

  1. shyam

    shyam Guest

    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]



    }
     
    shyam, Nov 29, 2005
    #1
    1. Advertising

  2. shyam

    Artie Gold Guest

    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!"
     
    Artie Gold, Nov 29, 2005
    #2
    1. Advertising

  3. 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>>
     
    Barry Schwarz, Nov 29, 2005
    #3
  4. shyam

    Jordan Abel Guest

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


    they're in what, now?
     
    Jordan Abel, Nov 29, 2005
    #4
  5. shyam

    Default User Guest

    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
     
    Default User, Nov 29, 2005
    #5
  6. shyam

    Jirka Klaue Guest

    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
     
    Jirka Klaue, Nov 29, 2005
    #6
  7. 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?
     
    Martin Ambuhl, Nov 29, 2005
    #7
  8. shyam

    Flash Gordon Guest

    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.
     
    Flash Gordon, Nov 29, 2005
    #8
  9. shyam

    Jordan Abel Guest

    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.
     
    Jordan Abel, Nov 29, 2005
    #9
  10. shyam

    Default User Guest

    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
     
    Default User, Nov 29, 2005
    #10
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?Q2xhcw==?=
    Replies:
    0
    Views:
    1,506
    =?Utf-8?B?Q2xhcw==?=
    Nov 10, 2005
  2. bit manuplation

    , Dec 18, 2004, in forum: Java
    Replies:
    3
    Views:
    617
    Chris Smith
    Dec 18, 2004
  3. Replies:
    4
    Views:
    3,561
  4. surendra

    bit fileds or bit manuplation

    surendra, Jun 23, 2006, in forum: C Programming
    Replies:
    2
    Views:
    296
  5. Kee Nethery
    Replies:
    12
    Views:
    2,160
    Stefan Behnel
    Jun 27, 2009
Loading...

Share This Page