This question already has answers here:
format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘int *’
                                
                                    (3个答案)
                                
                        
                                2年前关闭。
            
                    
我想将argv [1]转换为int。但是我得到这个警告:

xorcipher.c:7:9: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘int *’ [-Wformat=]


然后printf显示我

-799362156


如果我输入

./xorcipher 4


如何纠正呢?

这是我的代码:

#include <stdio.h>
#include <stdlib.h>

int main(int argc,char* argv[])
{
    int key_length = atoi(argv[1]);
    printf("key_length = %d", &key_length);
    return(0);
}

最佳答案

您正在传递key_length的地址,在错误状态中,printf仅期望该值。尝试这个:

printf("key_length = %d", key_length);


有关C格式说明符,请参见this tutorial

10-06 05:05