Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Basic C++ question

Reply
Thread Tools

Basic C++ question

 
 
CPlusPlus
Guest
Posts: n/a
 
      04-05-2010
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
 
Reply With Quote
 
 
 
 
Stefan Ram
Guest
Posts: n/a
 
      04-05-2010
CPlusPlus <(E-Mail Removed)> 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++.

 
Reply With Quote
 
 
 
 
Stefan Ram
Guest
Posts: n/a
 
      04-05-2010
"Leigh Johnston" <(E-Mail Removed)> 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.)

 
Reply With Quote
 
CPlusPlus
Guest
Posts: n/a
 
      04-05-2010
The difference is defined for some realms .. hehe :0)

On Apr 5, 9:12*am, "Leigh Johnston" <(E-Mail Removed)> wrote:
> "Stefan Ram" <(E-Mail Removed)-berlin.de> wrote in message
>
> news:(E-Mail Removed)-berlin.de...
>
> > CPlusPlus <(E-Mail Removed)> 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


 
Reply With Quote
 
Rui Maciel
Guest
Posts: n/a
 
      04-05-2010
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
 
Reply With Quote
 
Leclerc
Guest
Posts: n/a
 
      04-05-2010


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


http://www.parashift.com/c++-faq-lit....html#faq-18.5

cheers,
Gordan
 
Reply With Quote
 
Öö Tiib
Guest
Posts: n/a
 
      04-05-2010
On 5 apr, 17:13, Leclerc <(E-Mail Removed)> wrote:
> > 2. *What is the difference between const char *p and char* const p?

>
> http://www.parashift.com/c++-faq-lit....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.


 
Reply With Quote
 
CPlusPlus
Guest
Posts: n/a
 
      04-05-2010

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

Thanks all!

 
Reply With Quote
 
John H.
Guest
Posts: n/a
 
      04-05-2010
On Apr 5, 11:32*am, Öö Tiib <(E-Mail Removed)> wrote:
> On 5 apr, 17:13, Leclerc <(E-Mail Removed)> wrote:
>
> > > 2. *What is the difference between const char *p and char* const p?

>
> >http://www.parashift.com/c++-faq-lit....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".
 
Reply With Quote
 
Andrew Poelstra
Guest
Posts: n/a
 
      04-06-2010
On 2010-04-06, Jorgen Grahn <(E-Mail Removed)> 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
 
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
TurboTax Basic vs. Taxcut Basic? Sharp Dressed Man Computer Support 1 01-12-2009 12:52 PM
What is the difference between Visual Basic.NET and Visual Basic 6? Jimmy Dean Computer Support 3 07-25-2005 07:05 AM
Re: Python interpreter in Basic or a Python-2-Basic translator. rrr@ronadam.com Python 0 05-02-2005 01:48 PM
Python interpreter in Basic or a Python-2-Basic translator. Engineer Python 6 05-01-2005 10:16 PM
Upgrading Microsoft Visual Basic 6.0 to Microsoft Visual Basic .NET Jaime MCSD 2 09-20-2003 05:16 AM



Advertisments