c - C strncat 函数
问题描述
有人知道我的代码有什么问题吗?
#include <stdio.h>
#include <string.h>
#define MAX_LEN 10
void printMessage(char str[]);
int main(void)
{
char str[MAX_LEN] = "THANK ";
char you = 'u';
strncat(str, you, 1); // do not fix this line or the next one
printMessage(str);
return 0;
}
我得到的错误是:
strncat:此函数或变量可能不安全,请考虑使用 strnact_s。
但是,我想使用此功能strncat
。
解决方案
您可能收到此错误消息:
Error C4996 'strncat': This function or variable may be unsafe. Consider using strncat_s instead.
To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
要删除警告,您可以将其放在#define _CRT_SECURE_NO_WARNINGS
代码的开头。
此外,这是错误的:
char you = 'u';
因为of的第二个参数strncpy
是指向char而不是char的指针。将其更改为:
char you[] = "u";
作为替代方案,您也可以离开char you = 'u'
并strncat
像这样打电话:
strncat(str, &you, 1);
推荐阅读
- r - R计数具有重复值的行数
- java - 我如何从 json 数组中提取数据以用于后续的 rest 确保 api 请求
- json - 如何将 JSON 数据插入 Oracle 数据库?
- reactjs - Apollo Client 3 将反应变量重置为其初始值
- python - 除了员工考勤表上的错误外,如何修复尝试?
- python - 自动点检测
- c# - 有没有一种方法可以访问 appsettings.json 配置而不需要类中的构造函数我试图从中访问它
- javascript - 在 React 中如何确定事件所附加的元素?
- python - 将 3D 三角剖分曲面展开为平面 2D 图
- c - 使用 uint64_t 的不便之处