gpt4 book ai didi

C字符串函数错误

转载 作者:太空宇宙 更新时间:2023-11-04 02:28:17 24 4
gpt4 key购买 nike

我正在做作业,但我的代码有问题:

int is_ascii_match(const char c1[], const char c2[], char res[MAX_STRING_SIZE]) {
....

if (val_c1 == val_c2) {
strcpy(res, strcat(strcat(c1, '&'),c2));
}
else
return 0;

我收到一个错误:

Access violation reading location

我是否传递了错误的参数或..?

最佳答案

strcat 需要一个非常量 char*。您传递了 const,这就是编译器提示的原因。

第二个参数也是"&"。 (之前您传递了一个 char)。

来自标准 §7.24.3.1

char *strcat(char * restrict s1, const char * restrict s2);

The strcat function appends a copy of the string pointed to by s2 (including the terminating null character) to the end of the string pointed to by s1.

所以 s1 将被修改(第一个参数),这就是它应该是非常量的原因。

关于C字符串函数错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47656356/

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