1. I have a question in C++ about strings. For example I wrote this code:
char *temp,*token;
Then the temp returns "sihJay" but I want it to return just "sih". I mean I want to empty temp before copying. after the codes above, I want to reverse the temp by strrev and there, the problem will occure! I have tried all these codes to empty temp:
Some would give Error and some would make no change to temp. I want you to show me the way to empty temp just like when it was declared at the begining.
2. is it possible to have strtok with more than one second parameter? I mean, for example you have a sentence like "Hello! Is this a test, you know?" and you want to separate the words of it by " ","!","?",",",etc. So how would you do that?
Thanks a lot

bm, Thank you for your questions.

1. Your code uses pointer variables "temp" and "token" incorrectly. A pointer variable, as the name implies, is simply a variable containing the address of a memory location. To use a pointer variable correctly, you need to do two things: allocate some memory for program use, and set the pointer variable to the address of the allocated memory. Here is how to do this:

// This declares a pointer variable:
char * pMyString;

// This declares a memory block (a fixed-length array of characters):
char MyString[256];

// This sets the pointer to point to the first character of the string:
pMyString = MyString;

// At this point you can store a string
strcpy(pMyString, "Test");

// Or, you could avoid using the pointer entirely (pointers are not needed for simple operations):
strcpy(MyString, "Test");

// To empty the string, store a NUL character (\0) at offset 0 in the array:
*MyString = 0;

// This is equivalent:
MyString[0] = 0;

2. "strtok" only takes two parameters. The second is a list of recognized delimiters; in your case, it might be "!,?". If you want to search first for one delimiter, then for another, you have to do two separate searches.

David Spector


All Answers

Answers by Expert:

Ask Experts


David Spector


Highly knowledgeable in the C++ language, Visual C++ (MSVC), Windows API, documentation and other quality-assurance techniques, and debugging. Knowledgeable in MFC, COM, GUI design, and object-oriented design.


I have been a software engineer since 1965. I have been published. My specializations have been: biomedical programming, compiler implementation, and many kinds of Windows programming. I don't do Databases or other business-oriented stuff.

Windows?/DOS Developer's Journal, ACM SIGPLAN Notices, and Computer Science Press.

ICCP Systems Programming Certification
Master's degree equivalent in Computer Science

©2017 About.com. All rights reserved.