gpt4 book ai didi

c - 为什么 printf ("%02x"...) 改变输出?

转载 作者:太空宇宙 更新时间:2023-11-04 05:19:49 27 4
gpt4 key购买 nike

/*HASHING*/
unsigned char *do_hashing(unsigned char *buffer){
unsigned char outbuffer[20];
unsigned char output[20];
SHA1(buffer, strlen(buffer), outbuffer);

for (int i=0; i<20; i++) {
output[i]=outbuffer[i];
}

printf("The hash: ");

for (int i = 0; i < 20; i++) {
printf("%02x ", outbuffer[i]);
}

printf("\n");

return output;
}
/*HASHING*/

如果我删除 printf 函数,为什么这个函数会产生不同的输出(错误的输出)。例如:

./ftest
The hash: a1 2a 9c 6e 60 85 75 6c d8 cb c9 98 c9 42 76 a7 f4 8d be 73
The hash: a1 2a 9c 6e 60 85 75 6c d8 cb c9 98 c9 42 76 a7 f4 8d be 73
=with for-loop print

./ftest

The hash: 6c 08 40 00 00 00 00 00 0a 00 00 00 00 00 00 00 00 00 00 00
=without for-loop print

在这种情况下,我没有包含主函数或#includes,因为此函数内部发生错误。

最佳答案

您正在返回一个指向局部变量的指针 unsigned char output[20];,

函数结束后变量不存在导致未定义的行为。

关于c - 为什么 printf ("%02x"...) 改变输出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15662575/

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