| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1159 人关注过本帖
标题:关于Begining C中strtok_s函数讲解的疑问
只看楼主 加入收藏
luverose
Rank: 1
等 级:新手上路
帖 子:2
专家分:2
注 册:2015-1-15
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:2 
关于Begining C中strtok_s函数讲解的疑问
Begining C中文第五版第六章217页,例子6.7中对strtok_s使用了4个参数(str,str_size,delimiters,ptr)
代码如下:
程序代码:
// Program 6.7 Find all the words
#define __STDC_WANT_LIB_EXT1__ 1 // Make optional versions of functions available
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
int main(void)
{
char delimiters[] = " \".,;:!?)("; // Prose delimiters
char buf[100]; // Buffer for a line of keyboard input
char str[1000]; // Stores the prose to be tokenized
char* ptr = NULL; // Pointer used by strtok_s()
str[0] = '\0'; // Set 1st character to null
size_t str_len = sizeof(str);
size_t buf_len = sizeof(buf);
printf("Enter some prose that is less than %zd characters.\n"
"Terminate input by entering an empty line:\n", str_len);
// Read multiple lines of prose from the keyboard
while(true)
{
if(!gets_s(buf, buf_len)) // Read a line of input
{
printf("Error reading string.\n");
return 1;
}
if(!strnlen_s(buf, buf_len)) // An empty line ends input
break;
if(strcat_s(str, str_len, buf)) // Concatenate the line with str
{
printf("Maximum permitted input length exceeded.\n");
return 1;
}
}
printf("The words in the prose that you entered are:\n", str);
// Find and list all the words in the prose
unsigned int word_count = 0;
char * pWord = strtok_s(str, &str_len, delimiters, &ptr); // Find 1st word
if(pWord)
{
do
{
printf("%-18s", pWord);
if(++word_count % 5 == 0)
printf("\n");
pWord = strtok_s(NULL, &str_len, delimiters, &ptr); // 编译出错的地方,编译器报错不应该有4个参数
}while(pWord); // NULL ends tokenizing
printf("\n%u words found.\n", word_count);
}
else
printf("No words found.\n");
return 0;
}

我在微软官网文档上找到的的sample经过检验,可以顺利编译
代码如下:
程序代码:
// crt_strtok_s.c
// In this program, a loop uses strtok_s
// to print all the tokens (separated by commas
// or blanks) in two strings at the same time.
//

#include <string.h>
#include <stdio.h>

char string1[] =
    "A string\tof ,,tokens\nand some  more tokens";
char string2[] =
    "Another string\n\tparsed at the same time.";
char seps[]   = " ,\t\n";
char *token1 = NULL;
char *token2 = NULL;
char *next_token1 = NULL;
char *next_token2 = NULL;

int main( void )
{
    printf( "Tokens:\n" );

    // Establish string and get the first token:
    token1 = strtok_s( string1, seps, &next_token1);
    token2 = strtok_s ( string2, seps, &next_token2);

    // While there are tokens in "string1" or "string2"
    while ((token1 != NULL) || (token2 != NULL))
    {
        // Get next token:
        if (token1 != NULL)
        {
            printf( " %s\n", token1 );
            token1 = strtok_s( NULL, seps, &next_token1);
        }
        if (token2 != NULL)
        {
            printf("        %s\n", token2 );
            token2 = strtok_s (NULL, seps, &next_token2);
        }
    }
}

请问这个是什么原因呢?是书上写错了吗?

另外还有一个问题就:
strtok_s( NULL, seps, &next_token1);//第三个参数为什么要取地址符呢?
搜索更多相关主题的帖子: 中文 
2015-01-25 17:51
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9024
专家分:54030
注 册:2011-1-18
收藏
得分:10 
因为你书上讲的是“C”,用的也是C标准库中的函数
char* strtok_s( char* restrict s1, rsize_t* restrict s1max, const char* restrict s2, char** restrict ptr );

而微软官网上讲的是“VC”,用的也是VC私有的非标准函数
char* strtok_s( char* strToken, const char* strDelimit, char** context );
2015-01-26 08:55
wangle212
Rank: 2
等 级:论坛游民
威 望:1
帖 子:7
专家分:10
注 册:2015-1-26
收藏
得分:10 
因为这个函数传入的是指针变量
2015-01-26 09:53
快速回复:关于Begining C中strtok_s函数讲解的疑问
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.018280 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved