【问题标题】:Cannot Use Printf!?! (while performing hashing algorithm using mhash)不能使用 Printf!?! (同时使用 mhash 执行散列算法)
【发布时间】:2014-08-28 05:36:48
【问题描述】:

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

关于如何修复此错误的任何建议?

这是我的代码:

#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]

提前感谢您的帮助!

【问题讨论】:

  • 错误信息很清楚,你传递一个整数但printf需要一个字符串。
  • printf 需要一个字符串,如果需要,可以格式化它:printf("%d",length)

标签: c pointers hash format printf


【解决方案1】:

检查man page for printf()。第一个参数是const char *。你传递了一个int

警告也是这么说的:

警告:传递 'printf' 的参数 1 使指针来自整数,无需强制转换 [默认启用]

你想要的:

printf("%d", length);

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

【讨论】:

    猜你喜欢
    • 2014-08-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-09
    • 2021-01-20
    • 1970-01-01
    • 2014-08-25
    • 2016-03-15
    相关资源
    最近更新 更多