Basic C++ question

C

CPlusPlus

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
 
S

Stefan Ram

CPlusPlus said:
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++.
 
S

Stefan Ram

Leigh Johnston said:
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.)
 
C

CPlusPlus

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

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

Why static?


 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
 
C

CPlusPlus

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

Thanks all!
 
J

John H.

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".
 
A

Andrew Poelstra

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.
 
J

Jorgen Grahn

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


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
 

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,996
Messages
2,570,237
Members
46,825
Latest member
VernonQuy6

Latest Threads

Top