gpt4 book ai didi

无法使用Printf!?! (使用 mhash 执行哈希算法时)

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

我正在使用 Mhash,我想打印 block 大小的长度以用于调试目的,但每次尝试编译时我都会收到错误

关于如何修复此错误有什么建议吗?

这是我的代码:

#include <mhash.h>
#include <stdio.h>
#include <string.h>

// 0x12e6bc6e68c3b9506e6668db6b7224f894fab073728fc179 (TIGER192) (48)

int main()
{
char password[] = "Jefe";
int keylen = 4;
char data[] = "what do ya want for nothing?";
int datalen = 28;
MHASH td, td2;
unsigned char *mac, *mac2;
int i, j;

td = mhash_hmac_init(MHASH_TIGER192, password, keylen, mhash_get_hash_pblock(MHASH_TIGER192));

mhash(td, data, datalen);
mac = mhash_hmac_end(td);

printf("0x");
for (i = 0; i < mhash_get_block_size(MHASH_TIGER192); i++)
{
printf("%.2x", mac[i]);
}
printf("\n");

// int length = strlen(mac);
// printf(length);

// int length = 5;
// printf(length);

exit(0);
}

I run the program with the following commands:
hb2@hb1:~/Desktop$ gcc -o hashexample hashexample.c -lmhash
hb2@hb1:~/Desktop$ ./hashexample
0x12e6bc6e68c3b9506e6668db6b7224f894fab073728fc179

它运行成功,但是当我尝试打印哈希结果的长度时,出现以下错误!!?关于原因有什么想法吗?

// int length = strlen(mac);
// printf(length);

hb2@hb1:~/Desktop$ gcc -o hashexample hashexample.c -lmhash
hashexample.c: In function ‘main’:
hashexample.c:33:2: warning: passing argument 1 of ‘printf’ makes pointer from integer without a cast [enabled by default]
/usr/include/stdio.h:363:12: note: expected ‘const char * __restrict__’ but argument is of type ‘int’
hashexample.c:33:2: warning: format not a string literal and no format arguments [-Wformat-security]

一开始我以为是因为我以为我使用了strlen错误?!但即使当我尝试对整数执行简单的 printf 时,我仍然收到错误:

// int length = 5;
// printf(length);

hb2@hb1:~/Desktop$ gcc -o hashexample hashexample.c -lmhash
hashexample.c: In function ‘main’:
hashexample.c:35:2: warning: passing argument 1 of ‘printf’ makes pointer from integer without a cast [enabled by default]
/usr/include/stdio.h:363:12: note: expected ‘const char * __restrict__’ but argument is of type ‘int’
hashexample.c:35:2: warning: format not a string literal and no format arguments [-Wformat-security]

提前感谢您的帮助!

最佳答案

检查man page for printf() 。第一个参数是 const char * 。您正在传递 int .

警告也是如此:

warning: passing argument 1 of ‘printf’ makes pointer from integer without a cast [enabled by default]

你想要:

printf("%d", length);

您需要格式字符串来指定 int即将打印。

关于无法使用Printf!?! (使用 mhash 执行哈希算法时),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24615647/

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