# "dereferencing pointer to incomplete type" error in examplar code - linuxism?



## caesius (Nov 26, 2009)

Hi, I'm learning a bit of network programming over the holidays from this guide http://beej.us/guide/bgnet/output/html/singlepage/bgnet.html

The first full program won't compile

I have bolded the line cc doesn't like.


```
/*
** showip.c -- show IP addresses for a host gien on the command line
*/

#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <arpa/inet.h>

int
main(int argc, char *argv[]) {
  
  struct addrinfo hints, *res, *p;
  int status;
  char ipstr[INET_ADDRSTRLEN];
  
  if(argc != 2) {
    fprintf(stderr, "usage: showip <hostname>\n");
    return 1;
  }
  
  memset(&hints, 0, sizeof hints);
  hints.ai_family = AF_INET;
  hints.ai_socktype = SOCK_STREAM;
  
  if( (status = getaddrinfo(argv[1], NULL, &hints, &res) ) != 0) {
    fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(status) );
    return 2;
  }
  
  printf("IP addresses for %s:\n\n", argv[1]);
  
  for(p = res; p != NULL; p = p->ai_next) {
    void *addr;
    char *ipver;
    
    struct sockaddr_in *ipv4 = (struct sockaddr_in *)p->ai_addr;
    [B]addr = &(ipv4->sin_addr);[/B]
    ipver = "IPv4";
    
    inet_ntop(p->ai_family, addr, ipstr, sizeof ipstr);
    printf(" %s: %s\n", ipver, ipstr);
  }
  
  freeaddrinfo(res);
  
  return 0;
}
```

I get this error:


```
> cc -o showip showip.c
showip.c: In function 'main':
showip.c:40: error: dereferencing pointer to incomplete type
showip.c:50:2: warning: no newline at end of file
>
```

What gives? Who cares if the types incomplete, why can't I access a member of the struct?

Cheers for any help, (yes I've tried gcc).


----------



## expl (Nov 26, 2009)

Here is how I would do it:


```
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <netinet/in.h>

int
main(int argc, char *argv[]) {
  
  struct addrinfo hints, *res, *p;
  int status;
  char *ipstr;
  
  if(argc != 2) {
    fprintf(stderr, "usage: showip <hostname>\n");
    return 1;
  }
  
  memset(&hints, 0, sizeof hints);
  hints.ai_family = AF_INET;
  hints.ai_socktype = SOCK_STREAM;
  
  if( (status = getaddrinfo(argv[1], NULL, &hints, &res) ) != 0) {
    fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(status) );
    return 2;
  }
  
  printf("IP addresses for %s:\n\n", argv[1]);
  
  for(p = res; p != NULL; p = p->ai_next) {
    char *ipver;
    
    struct in_addr InAddr;
    ipver = "IPv4";
	
	memcpy(&InAddr, &p->ai_addr->sa_data[2], sizeof(InAddr));
    
	ipstr = inet_ntoa(InAddr);
	
    printf(" %s: %s\n", ipver, ipstr);
  }
  
  freeaddrinfo(res);
  
  return 0;
}
```


----------



## expl (Nov 26, 2009)

Or you could take your old example and add the missing definition for it to work like this:


```
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <arpa/inet.h>

[b]#include <netinet/in.h>[/b]

int
main(int argc, char *argv[]) {
  
  struct addrinfo hints, *res, *p;
  int status;
  char ipstr[INET_ADDRSTRLEN];
  
  if(argc != 2) {
    fprintf(stderr, "usage: showip <hostname>\n");
    return 1;
  }
  
  memset(&hints, 0, sizeof hints);
  hints.ai_family = AF_INET;
  hints.ai_socktype = SOCK_STREAM;
  
  if( (status = getaddrinfo(argv[1], NULL, &hints, &res) ) != 0) {
    fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(status) );
    return 2;
  }
  
  printf("IP addresses for %s:\n\n", argv[1]);
  
  for(p = res; p != NULL; p = p->ai_next) {
    void *addr;
    char *ipver;
    
    struct sockaddr_in *ipv4 = (struct sockaddr_in *)p->ai_addr;
    addr = &(ipv4->sin_addr);
    ipver = "IPv4";
    
    inet_ntop(p->ai_family, addr, ipstr, sizeof ipstr);
    printf(" %s: %s\n", ipver, ipstr);
  }
  
  freeaddrinfo(res);
  
  return 0;
}
```


----------



## caesius (Nov 26, 2009)

Thanks a lot, it works now. Why was that include not there in the example? I checked and it's definitely not.


----------

