2010-09-01 21 views
0

晚上好,大家。從不完整的指針類型分配

我現在遇到了一個奇怪的警告,對於我的生活我無法弄清楚它出錯的地方。我嘗試了許多不同的選項,我不記得了。

錯誤是:從兼容的指針類型

分配

相對代碼:

// Server structure 
typedef struct { 
    struct addrinfo address;  // Address info 
    char buffer[1024];    // Read biffer 
    fd_set connections;    // Current connections 
    int connections_max;   // Max file descriptor 
    int listener;     // Listener fd 
    int port;      // The port for the server to listen on 
    struct addrinfo socket_hints; // Server's socket hints 
    struct timeval socket_timeout; // When should the socket timeout? 
    struct User *users;    // Currently connected users 
} Server; 

// User structure 
typedef struct { 
    struct sockaddr_storage address; // User's address 
    socklen_t address_length;   // Length of users address 
    int fs_id;       // ID to the socket they belong to 
    char ip_address[INET6_ADDRSTRLEN]; // User's IP address 
    char *name;       // Pointer to the user's name 
    struct User *nextUser;    // Next user in the list 
} User; 

/** 
* Handles sockets on the provided server 
*/ 
void handle_sockets(Server *passed_server) { 
    int current_fd;    // Current file descriptor 
    int new_connection;   // Socket ID of new connections 
    fd_set read_sockets;  // Sockets to read from 
    FD_ZERO(&read_sockets); 
    struct timeval timeout;  // Sets the timeout for select 

    // See if we have sockets to read 
    read_sockets = passed_server->connections; 
    timeout = passed_server->socket_timeout; 
    if(select(passed_server->connections_max+1, &read_sockets, NULL, NULL, &timeout) == -1) { 
     perror("Selecting sockets"); 
     exit(1); 
    } 

    // Loop through all of the sockets 
    for(current_fd = 0; current_fd <= passed_server->connections_max; current_fd++) { 
     if(!FD_ISSET(current_fd, &read_sockets)) { 
      continue; 
     } 

     // Handle new connection 
     if(current_fd == passed_server->listener) { 
      User *new_user = malloc(sizeof *new_user); 
      memset(&new_user->address, 0, sizeof new_user->address); 

      // Get the new address 
      new_user->address_length = sizeof new_user->address; 
      new_user->fs_id = accept(passed_server->listener, (struct sockaddr *)&new_user->address, &new_user->address_length); 

      // Did we have an issue connecting? 
      if(new_user->fs_id == -1) { 
       free(new_user); 
       perror("Accepting new connection"); 
       continue; 
      } 

      // Add the user socket to the master list 
      FD_SET(new_user->fs_id, &passed_server->connections); 
      if(new_user->fs_id > passed_server->connections_max) { 
       passed_server->connections_max = new_user->fs_id; 
      } 

      // Add the user to the list 

      //******************* 
      // ERRORS IN THE NEXT TWO ASSIGNMENTS 
      //******************* 

      if(passed_server->users == NULL) { 
       passed_server->users = new_user; 
      } else { 
       new_user->nextUser = passed_server->users; 
       passed_server->users = new_user; 
      } 

      // Let them know we got one! 
      printf("Server: New connection from %s on socket %d. Send hello.\n", 
        inet_ntop(
          new_user->address.ss_family, 
          get_address((struct sockaddr*)&new_user->address), 
          new_user->ip_address, 
          INET6_ADDRSTRLEN 
        ), 
        new_user->fs_id 
      ); 

      // Can we get to the user from the server? 
      //printf("Repeat, the IP address is %s\n", passed_server->users->ip_address); 

      // Move on to the next file descriptor 
      continue; 
     } 
    } 
} 

Full Code

+0

警告來自哪條線? – 2010-09-01 02:16:47

+0

哪一個是警告:「不完整」(來自你的頭銜)還是「不相容的」(來自身體)?我猜「不完整」? – bstpierre 2010-09-01 02:22:55

+0

對不起。它來自passed_server-> users = new_user ;. – Codeacula 2010-09-01 02:26:40

回答

3
typedef struct User { 
    struct sockaddr_storage address; // User's address 
    socklen_t address_length;   // Length of users address 
    int fs_id;       // ID to the socket they belong to 
    char ip_address[INET6_ADDRSTRLEN]; // User's IP address 
    char *name;       // Pointer to the user's name 
    struct User *nextUser;    // Next user in the list 
} User; 

在頂部添加「struct User」,以便您可以在結構中引用它。

另外,您需要交換聲明的順序,以便Server結構知道User結構。

+0

謝謝,這似乎是一個竅門。 我曾考慮過在服務器之前放置用戶,但編譯器讓我像我在提供的代碼中一樣繼續我的快樂方式,所以我認爲一切都很好。謝謝您的幫助! – Codeacula 2010-09-01 02:27:44

+0

對於服務器來說,因爲它只能指向一個用戶,我真的需要把用戶放在第一位嗎?如果沒有(服務器只需要知道它指向的是用戶而不是用戶,對嗎?),那麼我可以將服務器/用戶的東西分離成單獨的源文件。 – Codeacula 2010-09-01 05:13:30

1

指針聲明指向一個特定類型的變量,並與外部如果您嘗試將已聲明爲指向一種類型的指針指定爲已聲明爲指向某種其他類型的指針,您將得到該警告。

指針需要有一個類型來解釋該位置的內存內容。例如,某個浮點指針需要知道讀取四個字節而不是兩個或三個字節。這些信息在指針類型中提供。

因此,請確保您分配兼容類型的指針,或者如果您確定要更改類型,請使用強制轉換。

+0

我原以爲我在做這件事,但正如bstpierre指出的那樣,我需要改變周圍的結構。 – Codeacula 2010-09-01 02:26:15

+0

很高興現在正在工作。 – WildCrustacean 2010-09-01 02:38:57

相關問題