gpt4 book ai didi

c - 警告 : assignment makes integer from pointer without a cast with chars and strings

转载 作者:行者123 更新时间:2023-11-30 15:45:28 24 4
gpt4 key购买 nike

我在这段代码上不断遇到段错误,我现在正在学习 C..有人可以帮助我吗?

错误位置:

char *m;
char *as = concat("helloworld", buf);
*m = sha1(as); <<<<<< as
printf("%s\n", m);
free(as);

concat 函数(不是我的,用于连接 2 个字符串):

char* concat(char *s1, char *s2)
{
size_t len1 = strlen(s1);
size_t len2 = strlen(s2);
char *result = malloc(len1+len2+1);//+1 for the zero-terminator
//in real code you would check for errors in malloc here
memcpy(result, s1, len1);
memcpy(result+len1, s2, len2+1);//+1 to copy the null-terminator
return result;
}

sha1 函数:

char *sha1( char *val ){

int msg_length = strlen( val );
int hash_length = gcry_md_get_algo_dlen( GCRY_MD_SHA1 );
unsigned char hash[ hash_length ];
char *out = (char *) malloc( sizeof(char) * ((hash_length*2)+1) );
char *p = out;
gcry_md_hash_buffer( GCRY_MD_SHA1, hash, val, msg_length );
int i;
for ( i = 0; i < hash_length; i++, p += 2 ) {
snprintf ( p, 3, "%02x", hash[i] );
}
return out;
}

既然我问,它们是什么意思:

 //in real code you would check for errors in malloc here

提前致谢

最佳答案

sha1() 返回 char *,因此需要将 *m = sha1(as); 更改为 m = sha(as);*m 的类型为 char,而不是 char *

关于c - 警告 : assignment makes integer from pointer without a cast with chars and strings,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19029546/

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