Here is my code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
int main (void) {
  struct addrinfo hints; 
  memset (&hints, 0, sizeof hints);
  hints.ai_family = AF_UNSPEC; 
  hints.ai_socktype = SOCK_DGRAM;  
  hints.ai_flags = AI_CANONNAME;   
  struct addrinfo *res;
  getaddrinfo ("example.com", "http", &hints, &res);
  printf ("Host: %s\n", "example.com");
  void *ptr;
  while (res != NULL) {
    printf("AI Family for current addrinfo: %i\n", res->ai_family);
    switch (res->ai_family) {
      case AF_INET:
        struct sockaddr_in *sockAddrIn = (struct sockaddr_in *) res->ai_addr;
        printf("Port number: %u\n", ntohs(sockAddrIn->sin_port));
        printf("IP address is: %u\n", ntohs(sockAddrIn->sin_addr.s_addr));
        break;
    }
    res = res->ai_next;
  }
  return 0;
}
And in action:
$ gcc ex3.c
$ ./a.out
Host: example.com
AI Family for current addrinfo: 2
Port number: 80
IP address is: 23992
AI Family for current addrinfo: 30
This is all fine, however this does not even compile:
void *ptr;
while (res != NULL) {
printf("AI Family for current addrinfo: %i\n", res->ai_family);
switch (res->ai_family) {
  case AF_INET:
    ptr = (struct sockaddr_in *) res->ai_addr;
    printf("Port number: %u\n", ntohs(ptr->sin_port));
    printf("IP address is: %u\n", ntohs(ptr->sin_addr.s_addr));
    break;
}
res = res->ai_next;
}
What I am trying to do is to cast a void pointer to a struct sockaddr_in *. The compile error is:
ex3.c:33:48: error: member reference base type 'void' is not a structure or union
printf("IP address is: %u\n", ntohs(ptr->sin_addr.s_addr));
What am I doing wrong? Why is the pointer ptr not casted? 
 
     
     
     
     
     
    