首页 > 解决方案 > 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

标签: c

解决方案


您可能收到此错误消息:

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);

推荐阅读