gpt4 book ai didi

c - 使用递归创建哈希码函数

转载 作者:行者123 更新时间:2023-11-30 19:45:31 25 4
gpt4 key购买 nike

我最初的论点有问题,但后来得到了解决。但是现在我收到了 seg failure 错误,并且不知道如何修复它。

这是我的代码

int hash_code(const char* str){
int len = strlen(str);
char dst[len-1];
if(len == 0 )
return 0;
else{
strncpy(dst, str, (len - 1));
return (hash_code(dst) * 27) + str[len-1];
}
}

最佳答案

" error: passing argument 1 of 'strncpy' discards 'const' qualifier from pointer target type"

此错误是由于 strncpy 获取的第一个参数的类型为 char* 但您传递的是 const char*。 strncpy原型(prototype)是

char * strncpy ( char * destination, const char * source, size_t num );
^^^^^^^^^^^^^^^^^^

第一个指针是指向目标的指针,而不是指向源的指针。

关于c - 使用递归创建哈希码函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26207767/

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