简体   繁体   English

strtok_s()为什么我们需要指针的地址?

[英]strtok_s() Why do we need address of a pointer?

I am back with C++ after 15 years...I just can't remember why we need the address of a pointer. 15年后我回到了C ++ ......我只是记不起为什么我们需要指针的地址。 Like in this statement: 就像在这个声明中:

char *next_token = NULL;
char *pszMozilla = strtok_s(szCopyVariable, "/", &next_token);

Is there an assumption that the address of the pointer will represent eventually the start of a list of pointers ? 是否假设指针的地址最终将表示指针列表的开头?

strtok_s is a reentrant function, and it needs to store some state somewhere. strtok_s是一个可重入的函数,它需要在某处存储一些状态。 That state is a pointer to the character one after the last one it processed. 该状态是指向处理完最后一个字符的字符的指针。 (Think about it, that's really all you need to resume tokenizing.) (想一想,这就是恢复令牌化所需要的全部。)

If a function wants to store an X in a user-provided space, the user needs to provide a pointer to an X, pointing to where the X will go. 如果函数想要在用户提供的空间中存储X,则用户需要提供指向X的指针,指向X将去往的位置。 In our case, X is a "pointer to char". 在我们的例子中,X是“指向char的指针”。

那是因为strtok_s()通过在每次调用时移动next_token维护状态。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM