Basic C++ question

Discussion in 'C++' started by CPlusPlus, Apr 5, 2010.

  1. CPlusPlus

    CPlusPlus Guest

    char* pStr = "This is a test";
    char* token = strtok(pStr, " "); // space as delimiter

    Above simple code throws an exception. I think I understand why.
    Pointer pStr points to a constant string.

    1. How can I tokenize pStr using pointers?
    2. What is the difference between const char *p and char* const p?

    TY
     
    CPlusPlus, Apr 5, 2010
    #1
    1. Advertising

  2. CPlusPlus

    Stefan Ram Guest

    CPlusPlus <> writes:
    >1. How can I tokenize pStr using pointers?


    #include <cstring>

    int main()
    { char const * pStr = "This is a test";
    static char text[ 15 ];
    strcpy( text, pStr );
    char * t = strtok( text, " " ); }

    >2. What is the difference between const char *p and char* const p?


    The difference is defined only for some realms,
    such as numbers (5-3=2), but one cannot calculate
    a difference for two type specifications in C++.
     
    Stefan Ram, Apr 5, 2010
    #2
    1. Advertising

  3. CPlusPlus

    Stefan Ram Guest

    "Leigh Johnston" <> writes:
    >>int main()
    >>{ char const * pStr = "This is a test";
    >> static char text[ 15 ];
    >> strcpy( text, pStr );
    >> char * t = strtok( text, " " ); }

    >Why static?


    I don't know.

    (Long version:

    I copied some parts of the code from ISO/IEC 9899:1999,
    including this »static«. I did not know a reason for this
    »static«, but to be on the safe side, I'd rather not delete
    it, as I could not see harm in it either and there might be
    some reason for it I did not know.

    One might say, that automatic storage duration is »needed«
    only for recursion and the function main shall not be used
    within a program, so it does not »need« automatic variables,
    but this is no good reason, since automatic storage does not
    have higher cost than static storage in this case.)
     
    Stefan Ram, Apr 5, 2010
    #3
  4. CPlusPlus

    CPlusPlus Guest

    The difference is defined for some realms .. hehe :0)

    On Apr 5, 9:12 am, "Leigh Johnston" <> wrote:
    > "Stefan Ram" <-berlin.de> wrote in message
    >
    > news:-berlin.de...
    >
    > > CPlusPlus <> writes:
    > >>1.  How can I tokenize pStr using pointers?

    >
    > > #include <cstring>

    >
    > > int main()
    > > { char const * pStr = "This is a test";
    > >  static char text[ 15 ];
    > >  strcpy( text, pStr );
    > >  char * t = strtok( text, " " ); }

    >
    > Why static?
    >
    >
    >
    > >>2.  What is the difference between const char *p and char* const p?

    >
    > >  The difference is defined only for some realms,
    > >  such as numbers (5-3=2), but one cannot calculate
    > >  a difference for two type specifications in C++.

    >
    > Funny.
    >
    > /Leigh
     
    CPlusPlus, Apr 5, 2010
    #4
  5. CPlusPlus

    Rui Maciel Guest

    CPlusPlus wrote:

    > 1. How can I tokenize pStr using pointers?


    You can write a proper lexer/tokenizer which recognizes the tokens you wish to access. For
    that particular task re2c is your friend.

    http://re2c.sourceforge.net/


    Hope this helps,
    Rui Maciel
     
    Rui Maciel, Apr 5, 2010
    #5
  6. CPlusPlus

    Leclerc Guest

    Leclerc, Apr 5, 2010
    #6
  7. CPlusPlus

    Öö Tiib Guest

    On 5 apr, 17:13, Leclerc <> wrote:
    > > 2.  What is the difference between const char *p and char* const p?

    >
    > http://www.parashift.com/c -faq-lite/const-correctness.html#faq-18.5


    Note that FAQ does not say that 'Fred const* p' and 'const Fred* p'
    are two forms to say same thing. Question was about 'const char *p'
    and FAQ does not discuss 'const Fred* p' at all. Technically it does
    not fully answer the question since it is first thing that confuses
    the people asking 'what is const' questions.
     
    Öö Tiib, Apr 5, 2010
    #7
  8. CPlusPlus

    CPlusPlus Guest

    Why indeed? To learn the inner working of string/characters
    manipulation etc.

    Thanks all!
     
    CPlusPlus, Apr 5, 2010
    #8
  9. CPlusPlus

    John H. Guest

    On Apr 5, 11:32 am, Öö Tiib <> wrote:
    > On 5 apr, 17:13, Leclerc <> wrote:
    >
    > > > 2.  What is the difference between const char *p and char* const p?

    >
    > >http://www.parashift.com/c -faq-lite/const-correctness.html#faq-18.5

    >
    > Note that FAQ does not say that 'Fred const* p' and 'const Fred* p'
    > are two forms to say same thing. Question was about 'const char *p'
    > and FAQ does not discuss 'const Fred* p' at all. Technically it does
    > not fully answer the question since it is first thing that confuses
    > the people asking 'what is const' questions.


    Yes, it might be helpful for the reader to read the first part of FAQ
    18.9 to learn that "const char * p" and "char const * p" are the
    same. Then FAQ 18.5 could be applied to learn the difference of
    "const char * p" and "char * const p".
     
    John H., Apr 5, 2010
    #9
  10. On 2010-04-06, Jorgen Grahn <> wrote:
    >
    > Agreed. strtok() is hateful. Don't use it in C++ code.
    >


    FWIW, you shouldn't use it in C code, either.

    > (Pedantic mode: It's not really static state. strtok() modifies the
    > char array it tokenizes, so it cannot be used with std::string or
    > std::vector<char>. Hard to use in a sane way.)
    >


    There is a static state to it as well - if you pass it NULL,
    then it'll remember the last string that it tokenized, which
    causes all sorts of re-entrancy problems.

    --
    Andrew Poelstra
    http://www.wpsoftware.net/andrew
     
    Andrew Poelstra, Apr 6, 2010
    #10
  11. CPlusPlus

    Jorgen Grahn Guest

    On Tue, 2010-04-06, Andrew Poelstra wrote:
    > On 2010-04-06, Jorgen Grahn <> wrote:
    >>
    >> Agreed. strtok() is hateful. Don't use it in C++ code.
    >>

    >
    > FWIW, you shouldn't use it in C code, either.
    >
    >> (Pedantic mode: It's not really static state. strtok() modifies the
    >> char array it tokenizes, so it cannot be used with std::string or
    >> std::vector<char>. Hard to use in a sane way.)
    >>

    >
    > There is a static state to it as well - if you pass it NULL,
    > then it'll remember the last string that it tokenized, which
    > causes all sorts of re-entrancy problems.


    Oops, I forgot about that. I only learned strtok() to be able to
    remove it from our code base at work. I guess a selective memory is a
    good thing in this case.

    /Jorgen

    --
    // Jorgen Grahn <grahn@ Oo o. . .
    \X/ snipabacken.se> O o .
     
    Jorgen Grahn, Apr 10, 2010
    #11
    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. Stefan Duenser

    Basic shifting question

    Stefan Duenser, Dec 7, 2004, in forum: VHDL
    Replies:
    4
    Views:
    443
    Stefan Duenser
    Dec 8, 2004
  2. Replies:
    1
    Views:
    582
    Eric Smith
    Jul 8, 2005
  3. Engineer
    Replies:
    6
    Views:
    627
    Jeremy Bowers
    May 1, 2005
  4. Replies:
    0
    Views:
    438
  5. Ark Khasin

    A basic question question about volatile use

    Ark Khasin, Jul 31, 2008, in forum: C Programming
    Replies:
    12
    Views:
    360
    Ark Khasin
    Jul 31, 2008
Loading...

Share This Page