gpt4 book ai didi

c++ - C++ 是否支持 strnlen 函数,在 codeblocks ide 上?

转载 作者:太空狗 更新时间:2023-10-29 21:39:33 24 4
gpt4 key购买 nike

我的程序是:

#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;
}

错误是:

 strnlen was not declared in this scope

问题:

  1. 为什么我无法在我的 codeblocks IDE 上使用 C++ 中的 strnlen 函数?

  2. 它们是 strnlen 的良好替代品吗?

最佳答案

strnlen 是一个 GNU 扩展,也在 POSIX (IEEE Std 1003.1-2008) 中指定。 .如果 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;
}

我回答过类似的问题here .

关于c++ - C++ 是否支持 strnlen 函数,在 codeblocks ide 上?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32676369/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com