0

编写服务器/客户端程序并在使用 Netbeans 构建时遇到这些错误:

dbserver.c:195: warning: embedded '\0' in format
dbserver.c:211: warning: passing argument 1 of 'db_lookup' from incompatible pointer type
dbserver.c:213: warning: passing argument 1 of 'db_insert' from incompatible pointer type
dbserver.c:241: warning: passing argument 1 of 'db_delete' from incompatible pointer type
dbserver.c:257: warning: passing argument 1 of 'db_lookup' from incompatible pointer type
dbserver.c:287: warning: embedded '\0' in format

这是有问题的代码:

//------------------------------------------------------------------------------
//                            Static Definitions
//------------------------------------------------------------------------------

#define WORD_LENGTH 32 //size of transmission & word length (31 characters + \0)        
#define DICTIONARY_SIZE 100 //size of dictionary word count



//------------------------------------------------------------------------------
//                       Message Structure Definition
//------------------------------------------------------------------------------

typedef struct message 
{
    int msg_type; //message code used to call database functions
    int msg_size; //expected size of message in transit
    char msg[32]; //payload capacity
} message;



//------------------------------------------------------------------------------
//                Server Function & Variable Initialization 
//------------------------------------------------------------------------------

char dictionary[DICTIONARY_SIZE][WORD_LENGTH]; //dictionary structure
int word_count = 0; //dictionary word counter   
int db_initialization(); //create db dictionary
int db_insert(const char *word); //check for word, insert if not found
int db_delete(const char *word); //remove word from dictionary if found
int db_lookup(const char *word); //search dictionary for word
int db_function(int fd); //db function call interface
int errexit(const char *format, ...); //Khalid's error and exit
//establishes socket to listen for connections
int listen_socket_TCP(const char *service, int qlen); 


//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
//                            Main Function
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------

int main(int argc, char *argv[])
{
    char *service = "27792"; //"Magic Port" to listen on
    struct sockaddr_in client_address; //IPv4 client address
    unsigned int client_address_length; //IPv4 client address length
    int master_socket; //TCP socket
    int slave_socket; //TCP socket spawned off of Master

    master_socket = listen_socket_TCP(service, WORD_LENGTH); //build database



//------------------------------------------------------------------------------
//        Dictionary Code Adapted from Material provided by Khalid
//------------------------------------------------------------------------------


    db_initialization(); //initialize the database
    //seed the database
    db_insert("hello"); 
    db_insert("communication");
    db_insert("depaul");
    db_insert("cdm");

    //--------------------------------------------------------------------------
    //                   Test Database Function Operation
    //--------------------------------------------------------------------------
    if (db_lookup("cti"))
        printf("found\n");
    //end if

    else 
    {
        db_insert("cti");
        printf("'cti' has been inserted\n");
    } //end else

    if (db_delete("cdm"))
        printf("'cdm' has been successfully deleted\n");
    //end if

    else
        printf("cannot find 'cdm'.");
    //end else



    //--------------------------------------------------------------------------
    //                Forever Loop to Listen for Connections
    //--------------------------------------------------------------------------
    while (1) 
    {
    client_address_length = sizeof(client_address);
    slave_socket = accept(master_socket, (struct sockaddr *)&client_address, 
                                &client_address_length);

        if (slave_socket < 0) 
        errexit("accept: %s\n", strerror(errno));
        //end if

    printf("Connecting...\n");
    db_function(slave_socket);
    close(slave_socket);
    } //end while

}
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
//                              END MAIN FUNCTION
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------





//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
//                            Server Function Codes:
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------



//------------------------------------------------------------------------------
//                       Execute Database Functionality
//------------------------------------------------------------------------------

int db_function(int fd)
{   
    int finished = 0;
    char re[2];
    int x = -1;

    while (!finished) 
    {
    char buf[sizeof(message)];
    int cc;
    struct message *sptr;

        message s;

        cc = recv(fd, buf, sizeof(message), 0);
    printf("Received Bytes: %i\n", cc);

        sptr = (struct message *) buf;

        memcpy(&s, sptr,sizeof(message));

        if (s.msg_size != strlen(s.msg))
        {
        x = -1;
        sprintf(re,"%d \0", x);
        send(fd, re, 2, 0);
        break;
    } //end if


        //----------------------------------------------------------------------
        //           Message Code 1 - Insert Word into Dictionary
        //
        // Return  1: Successfully Inserted Word
        // Return  0: Word Exists in Dictionary Already
        // Return -1: Failed to Add Word to Dictionary
        //----------------------------------------------------------------------

    if (s.msg_type == 1)
        {
        if ((db_lookup(&s.msg)) == 0) //search for preexisting word
            {
            if ((db_insert(&s.msg)) > 0) //insert word into dictionary
                {
            x = 1; //word added successfully
        } //end insert if

                else
                    x = -1;//unsuccessful in adding word

        } //end search and insert if

            else 
        x = 0;

            printf("Adding to dictionary: %s result %i\n", s.msg, x);
    } //end if message type 1



        //----------------------------------------------------------------------
        //           Message Code 2 - Delete Word from Dictionary
        //
        // Return  1: Successfully Deleted Word
        // Return  0: Word does not Exist in Dictionary
        // Return -1: Failed to Delete Word from Dictionary
        //----------------------------------------------------------------------

        if (s.msg_type == 2)
        {
        x = db_delete(&s.msg);
        printf("Deleting from dictionary: %s result %i\n", s.msg, x);
    } //end if message type 2



        //----------------------------------------------------------------------
        //           Message Code 3 - Lookup Word in Dictionary
        //
        // Return  1: Successfully Found Word
        // Return  0: Word not FOund in Dictionary
        // Return -1: Function Failed
        //----------------------------------------------------------------------

    if (s.msg_type == 3)
        {
        x = db_lookup(&s.msg);
        printf("Looking up from dictionary: %s result %i\n", s.msg, x);
    } //end if message type 3



        //----------------------------------------------------------------------
        //           Message Code 4 - Close Connection
        //----------------------------------------------------------------------

        if (s.msg_type == 4)
        {
        x = 1;

            printf("Closing connection: %s result %i\n", s.msg, x);

            break;
    } //end if message type 4


        printf("Words in dictionary: {");

        int c = 0;
        for (c=0; c < word_count; c++)
        {
        printf("%s, ", dictionary[c]);
    }

        printf("}\n");

        sprintf(re,"%d\0", x);

        send(fd, re, 2, 0);
    } // end while(!finished)

    return 0;
} //end db_function
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------





//------------------------------------------------------------------------------
//                         Database Initialization
//------------------------------------------------------------------------------

int db_initialization()
{
    word_count = 0;
    return 1;
} //end db_initialization
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------





//------------------------------------------------------------------------------
//                       Insert Word into Dictionary
//------------------------------------------------------------------------------
int db_insert(const char *word)
{
    strcpy(dictionary[word_count], word);
    word_count++;
    return word_count;
} //end db_insert
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------





//------------------------------------------------------------------------------
//                       Delete Word from Dictionary
//------------------------------------------------------------------------------
int db_delete(const char *word)
{
    int i;
    for (i=0 ; i < word_count ; i++)
    {
    if (strcmp(word, dictionary[i]) == 0) 
        {
        word_count--;

            strcpy(dictionary[i], dictionary[word_count]);

            return 1;
        } //end if

    } //end for

    return 0;
} //end db_delete
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------





//------------------------------------------------------------------------------
//                        Lookup Word in Dictionary
//------------------------------------------------------------------------------
int db_lookup(const char *word)
{
    int i;
    for (i=0 ; i<word_count ; i++)
    {
    if (strcmp(word, dictionary[i]) == 0)
        return 1;
    } //end for

    return 0;
} //end db_lookup
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------

我已经排除了预处理器指令和 listen_socket_TCP 函数(在另一个源文件中)。

4

1 回答 1

1

首先,这些是警告,而不是错误......

无论如何,看起来问题在于您如何传递字符串: dbserver.c:211: warning: passing argument 1 of 'db_lookup' from incompatible pointer type

正在db_lookup()寻找一个const char *例如在这里使用的时候:

 if (db_lookup("cti"))

你正在传递它:

   if ((db_lookup(&s.msg)) == 0) //search for preexisting word 

结构中 char 指针的地址,如果你只是传递它:

   if ((db_lookup(s.msg)) == 0) //search for preexisting word 

那应该可以解决这种类型的警告。

其次,\0从 sprintf 中删除 s 以消除以下两个警告: warning: embedded '\0' in format

于 2012-10-23T19:46:43.073 回答