#include <stdio.h>
#include <iostream>
#include <string.h>
using namespace std;
/*
void print_tokens( char *line )
{
static char whitespace[] = " \t\f\r\v\n";
char *token ;
for( token = strtok( line, whitespace ); token != NULL ; token = strtok( NULL, whitespace ) )
cout<<"Next token is "<< token<<endl;
}
*/
void print_tokens(const char *line )
{
static char whitespace[] = " \t\f\r\v\n";
char *token;
char *linetmp = (char *)malloc(sizeof(char) * (strlen(line)+1));//分配内存
strcpy(linetmp, line);//复制一个副本
for( token = strtok(linetmp, whitespace); token != NULL ; token = strtok( NULL, whitespace ) )
{
cout<<"Next token is "<< token<<endl;
}
free(linetmp);
}
/*先看帖子:http://blog.csdn.net/libuding/article/details/5870089
**再看:http://www.cnblogs.com/hoys/archive/2011/09/19/2180999.html
*/
void main()
{
char str2[] ="hello world we";
print_tokens( str2 );
/************************************************************************************************
char *str1 ={ "hello world we"};
print_tokens( str2 );
**会出现内存问题,非常难理解!!!
**,当使用char *test2 = "feng,ke,wei"作为第一个参数传入时,在位置①处, 由于test2指向的内容保存在文
**字常量区,该区的内容是不能修改的,所以会出现内存错误. 而char test1[] = "feng,ke,wei" 中的test1指
**向的内容是保存在栈区的,所以可以修改.
**************************************************************************************************/
system("pause");
}
#include <iostream>
#include <string.h>
using namespace std;
/*
void print_tokens( char *line )
{
static char whitespace[] = " \t\f\r\v\n";
char *token ;
for( token = strtok( line, whitespace ); token != NULL ; token = strtok( NULL, whitespace ) )
cout<<"Next token is "<< token<<endl;
}
*/
void print_tokens(const char *line )
{
static char whitespace[] = " \t\f\r\v\n";
char *token;
char *linetmp = (char *)malloc(sizeof(char) * (strlen(line)+1));//分配内存
strcpy(linetmp, line);//复制一个副本
for( token = strtok(linetmp, whitespace); token != NULL ; token = strtok( NULL, whitespace ) )
{
cout<<"Next token is "<< token<<endl;
}
free(linetmp);
}
/*先看帖子:http://blog.csdn.net/libuding/article/details/5870089
**再看:http://www.cnblogs.com/hoys/archive/2011/09/19/2180999.html
*/
void main()
{
char str2[] ="hello world we";
print_tokens( str2 );
/************************************************************************************************
char *str1 ={ "hello world we"};
print_tokens( str2 );
**会出现内存问题,非常难理解!!!
**,当使用char *test2 = "feng,ke,wei"作为第一个参数传入时,在位置①处, 由于test2指向的内容保存在文
**字常量区,该区的内容是不能修改的,所以会出现内存错误. 而char test1[] = "feng,ke,wei" 中的test1指
**向的内容是保存在栈区的,所以可以修改.
**************************************************************************************************/
system("pause");
}