不能使用Printf!?! (使用mhash执行散列算法时)

时间:2014-07-07 16:44:21

标签: c pointers hash format printf

我正在使用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]

提前感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

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

警告也是这样说的:

  
    

警告:传递参数1'printf'使得整数的指针没有强制转换[默认启用]

  

你想:

printf("%d", length);

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