请谁能帮我解决unix下的这个错误

时间:2013-11-15 14:22:28

标签: c unix

client.c:60:17: warning: format '%d' expects argument of type 'int', but argument 2 has type 'int *' [-Wformat]

client.c:63:17: warning: format '%d' expects argument of type 'int', but argument 2 has type 'int *' [-Wformat]

这是代码:

#include <stdio.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>

int main(int argc , char ** argv)
{

        if(argc != 3)  //check for parameters
        {
                printf("usage : a.out <IP=127.0.0.1> <Port number=22033>\n");
                return 0;
        }
    //int x;
        int             sockfd , port = 0 ;
        int            intger[101] , buff[101];
        struct sockaddr_in      servaddr;

        port = atoi(argv[2]);  //convert port number from string to integer


        bzero(&servaddr,sizeof(servaddr));  //reset the servaddr
        servaddr.sin_family = AF_INET;
        servaddr.sin_port   = htons(port);
        inet_pton(AF_INET, argv[1], &servaddr.sin_addr);

        sockfd = socket(AF_INET, SOCK_STREAM, 0);  //new socket

        bzero(buff,101);  //reset the buffer
    bzero(intger,101);  //





connect(sockfd, (struct sockaddr *) &servaddr, sizeof(servaddr)); //connect to the server
                bzero(buff,101); //reset the buffer


//for(;;){

                printf("Please Enter an Integer :");

                scanf("%d", intger); //reading from the user

                write(sockfd , intger , 300);   //sending the integer to the server

                printf("Sending %d to the server\n" , intger);

                printf("Waiting for server reply ...\n");

                read(sockfd , buff , 300);

                printf("Received %d from the server\n" , buff);

                //bzero(buff,300);

                printf("\n");
//}

        return 0;
}

2 个答案:

答案 0 :(得分:3)

我猜你正在做这样的事情printf("%d",x);,因为你还没有发布任何代码。

在上面的示例中,您似乎将x作为int*传递。 printf期待int

编辑:查看您的代码,您正在使用printf并传递intgr,但intgr是一个数组。

所以,对于你的代码行:

printf("Sending %d to the server\n" , intger);

应该是这样的(打印数组中的第一项):

printf("Sending %d to the server\n" , intger[0]);

答案 1 :(得分:0)

不会printf(“%d”,&amp; x);

工作?如果它是指向int的指针,它指向的数据是int,还是我完全忘记了我所知道的所有C?