Thread: Converting void* to rtnl_link_stats

  1. #1
    Registered User
    Join Date
    Jan 2014
    Posts
    139

    Converting void* to rtnl_link_stats

    Hello I took the code from sysinfo(2) - Linux manual page and I have made no modifications but I am getting this error

    Code:
    error: invalid conversion from ‘void*’ to ‘rtnl_link_stats*’ [-fpermissive]|
    on this line
    Code:
    struct rtnl_link_stats *stats = ifa->ifa_data;
    Code:
    #define _GNU_SOURCE     /* To get defns of NI_MAXSERV and NI_MAXHOST */
           #include <arpa/inet.h>
           #include <sys/socket.h>
           #include <netdb.h>
           #include <ifaddrs.h>
           #include <stdio.h>
           #include <stdlib.h>
           #include <unistd.h>
           #include <linux/if_link.h>
    
           int main(int argc, char *argv[])
           {
               struct ifaddrs *ifaddr, *ifa;
               int family, s, n;
               char host[NI_MAXHOST];
    
               if (getifaddrs(&ifaddr) == -1) {
                   perror("getifaddrs");
                   exit(EXIT_FAILURE);
               }
    
               /* Walk through linked list, maintaining head pointer so we
                  can free list later */
    
               for (ifa = ifaddr, n = 0; ifa != NULL; ifa = ifa->ifa_next, n++) {
                   if (ifa->ifa_addr == NULL)
                       continue;
    
                   family = ifa->ifa_addr->sa_family;
    
                   /* Display interface name and family (including symbolic
                      form of the latter for the common families) */
    
                   printf("%-8s %s (%d)\n",
                          ifa->ifa_name,
                          (family == AF_PACKET) ? "AF_PACKET" :
                          (family == AF_INET) ? "AF_INET" :
                          (family == AF_INET6) ? "AF_INET6" : "???",
                          family);
    
                   /* For an AF_INET* interface address, display the address */
    
                   if (family == AF_INET || family == AF_INET6) {
                       s = getnameinfo(ifa->ifa_addr,
                               (family == AF_INET) ? sizeof(struct sockaddr_in) :
                                                     sizeof(struct sockaddr_in6),
                               host, NI_MAXHOST,
                               NULL, 0, NI_NUMERICHOST);
                       if (s != 0) {
                           printf("getnameinfo() failed: %s\n", gai_strerror(s));
                           exit(EXIT_FAILURE);
                       }
    
                       printf("\t\taddress: <%s>\n", host);
    
                   } else if (family == AF_PACKET && ifa->ifa_data != NULL) {
                       struct rtnl_link_stats *stats = ifa->ifa_data;
    
                       printf("\t\ttx_packets = %10u; rx_packets = %10u\n"
                              "\t\ttx_bytes   = %10u; rx_bytes   = %10u\n",
                              stats->tx_packets, stats->rx_packets,
                              stats->tx_bytes, stats->rx_bytes);
                   }
               }
    
               freeifaddrs(ifaddr);
               exit(EXIT_SUCCESS);
           }
    Thanks in advance for the help

  2. #2
    Registered User
    Join Date
    Jun 2015
    Posts
    1,643
    It's because you're compiling C code as C++. C allows implicit casts of void* to other pointer types whilst C++ does not. If you feel compelled to compile it as C++ then you'll need to add a cast.
    Code:
    struct rtnl_link_stats *stats = static_cast<rtnl_link_stats*>(ifa->ifa_data);

  3. #3
    Registered User
    Join Date
    Jan 2014
    Posts
    139
    THANK YOU very much!!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. error: invalid conversion from 'const void*' to 'void*'
    By Wahidin Wahid in forum C++ Programming
    Replies: 10
    Last Post: 04-17-2012, 02:17 AM
  2. error invalid conversion from ‘const void*’ to ‘void*’
    By Wahidin Wahid in forum C Programming
    Replies: 3
    Last Post: 03-27-2012, 08:18 PM
  3. Replies: 12
    Last Post: 03-27-2009, 02:36 PM
  4. Converting an int main function to void()
    By stevedawg85 in forum C++ Programming
    Replies: 2
    Last Post: 03-04-2006, 02:38 PM
  5. Invalid conversion from 'const void*' to 'void*' error
    By prawntoast in forum C Programming
    Replies: 3
    Last Post: 05-01-2005, 10:30 AM

Tags for this Thread