C++ C+;中是否支持strnlen函数+;,在ide上?
我的计划是:C++ C+;中是否支持strnlen函数+;,在ide上?,c++,c++11,C++,C++11,我的计划是: #include <cstdio> #include <cstring> using namespace std; int main(int argc,char **argv) { const static size_t maxbuf=128; const char * s1="String one"; char sd1[maxbuf]; printf("length is %ld",strnlen(sd1,maxbuf))
#include <cstdio>
#include <cstring>
using namespace std;
int main(int argc,char **argv)
{
const static size_t maxbuf=128;
const char * s1="String one";
char sd1[maxbuf];
printf("length is %ld",strnlen(sd1,maxbuf));
return 0;
}
问题:
< > >为什么我不能在C++上使用我的代码块IDE?< /P> 中的<代码> STRNLLN < /代码>函数?
strnlen
的好替代品吗strnlen
是GNU扩展,也在中指定。如果strnlen
不可用(不支持此扩展时可能会发生这种情况),请使用以下替换
// Use this if strnlen is missing.
size_t strnlen(const char *str, size_t max)
{
const char *end = memchr (str, 0, max);
return end ? (size_t)(end - str) : max;
}
我回答了一个类似的问题。您遇到了什么错误?请检查strnlen function的定义位置并包含该库。为什么不使用-s?strnlen()是一个posix函数,cstring只支持标准的C库函数。为什么不使用
std::string
和std::cout
等?谢谢你,但是你能给我推荐一个windows中的编译器(或IDE)吗?是否有类似的替代方案使其在代码块ide上工作?实际上,它在我的Mac和xcode中工作得非常好,没有任何进一步的实现。IDE!=编译器!=链接器!=图书馆。这与编译器或IDE无关。只是因为它们有时是捆绑的(例如VS附带了编译器和链接器,…),这并不意味着它们是相同的。IDE只是一个环境,您可以在其中编写源代码,并可以设置构建链,而不是其他。当扩展在您的平台上不可用时,为什么不使用我发布的函数呢?一般的经验法则是尽可能依赖标准库。您正在处理遗留代码吗?您是否需要处理c字符串而不是std::string
?
// Use this if strnlen is missing.
size_t strnlen(const char *str, size_t max)
{
const char *end = memchr (str, 0, max);
return end ? (size_t)(end - str) : max;
}