LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-29-2010, 12:21 PM   #1
zak100
Member
 
Registered: Jul 2009
Posts: 262

Rep: Reputation: 2
Dereferencing pointer error during compilation


Hi,
I have got following code from a book:
Code:
#include <sys/types.h>

#include <sys/socket.h>

#include <netinet/in.h>

#include <string.h>

#include <stdlib.h>


#define PORTNUMBER  12345


main(){

	char buf[1024];

	int n, s, ns, len;

	struct sockaddr_in  name;
        char hostname[64];

	struct hostent *hp;


	if(gethostname(hostname, sizeof(hostname)) < 0) {

		perror("gethostname");

		exit(1);

	}


	if(hp= gethostbyname(hostname)) == NULL) {

		fprintf(stderr, "unknown host: %s. \n", hostname);

		exit(1);

	}



	if((s=socket(AF_INET, SOCK_STREAM, 0)) < 0) {

		perror ("socket");

		exit(1);

	}


	memset(&name, 0, sizeof(struct sockaddr_in));

	name.sin_family = AF_INET;

	name.sin_port=htons(PORTNUMBER);

	memcpy(&name.sin_addr, hp->h_addr_list[0], hp->h_length);

	len=sizeof(struct sockaddr_in);


	if(connect(s, (struct  sockaddr *) &name, len) < 0) {

		perror("connect");

		exit(1);

	}

        while((n=read(0, buf, sizeof(buf))) > 0) {

		if(send(s, buf, n, 0) < 0) {

			perror ("send");

			exit(1);

		}

	}

	close(s);

	exit(0);

}
When I am compiling this, I am getting following error:
Dereferencing pointer to incomplete type on the following line:
Code:
memcpy(&name.sin_addr, hp->h_addr_list[0], hp->h_length);
Can somebody plz help me with this?


Zulfi.

Last edited by zak100; 09-29-2010 at 12:23 PM.
 
Old 09-29-2010, 01:27 PM   #2
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,225

Rep: Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320
Quote:
Originally Posted by zak100 View Post
plz
This writing style is not appropriate here.
 
Old 09-29-2010, 01:42 PM   #3
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
This line
Code:
	struct hostent *hp;
is both a forward declaration of the struct hostent and a declaration/definition of the pointer hp. But there is no definition of struct hostent.

C, unfortunately, lets you use functions such as gethostname, without declaring them. So when you fail to include the required .h file, you don't get a very clear error message for not defining hostent and even less indication of not declaring gethostname, etc.

I did a quick google search of those symbols and found pages saying the required .h file is

netdb.h

But those pages said "BSD" and didn't say "Linux" so I'm not certain netdb.h is the correct include file for you in Linux (I think it is). I'm also not certain you're asking the question for Linux, not Windows. But anyway, you need to include the right .h file and that is probably netdb.h

It is important to know that "incomplete type" means it has been declared but not defined.

Last edited by johnsfine; 09-29-2010 at 01:55 PM.
 
Old 09-29-2010, 02:08 PM   #4
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by johnsfine View Post
...
I did a quick google search of those symbols and found pages saying the required .h file is

netdb.h

But those pages said "BSD" and didn't say "Linux" so I'm not certain netdb.h is the correct include file for you in Linux (I think it is).
...
On my Linux box:

Code:
sergei@amdam2:~> man -k hostent
endhostent (3)       - get network host entry
endhostent (3p)      - network host database functions
freehostent (3)      - get network hostnames and addresses
gethostent (3)       - get network host entry
gethostent (3p)      - network host database functions
gethostent_r (3)     - get network host entry
Net::hostent (3pm)   - by-name interface to Perl's built-in gethost*() functions
sethostent (3)       - get network host entry
sethostent (3p)      - network host database functions
sergei@amdam2:~>
- yes, it's also

Code:
#include <netdb.h>
.
 
Old 09-29-2010, 02:50 PM   #5
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by johnsfine View Post
...
C, unfortunately, lets you use functions such as gethostname, without declaring them.
...

Well, the real problem is "C" books for beginners. A book for beginner should teach compilation with the most strict set of compiler switches.

A good start WRT compiler switches is

-Wall -Wextra -Wformat=2 -Wmissing-prototypes

and a good place to start from reading about such switches is:

http://gcc.gnu.org/onlinedocs/gcc-4.4.4/gcc.pdf ->
3.8 Options to Request or Suppress Warnings
.
 
Old 09-30-2010, 12:21 AM   #6
zak100
Member
 
Registered: Jul 2009
Posts: 262

Original Poster
Rep: Reputation: 2
Hi,
Actually, I dont know about the appropriate style you want. I just copied the code and pasted it within the code tags.

Thanks for help. Its working now.Its great because there is less unix/linux culture in my country.

Zulfi.
 
Old 09-30-2010, 07:34 AM   #7
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by zak100 View Post
I dont know about the appropriate style you want.
Dugan was objecting to the way you abbreviated "please".

Many of the experts here object to posts that are full of such abbreviations. But I can't see the point of caring about a single instance in an otherwise appropriate post.

You'll notice Dugan's objection did not interfere with your getting the answer you needed, so it's best to just ignore. (I'm only explaining all this because his objection seems to have confused you).
 
Old 07-14-2014, 12:40 AM   #8
zak100
Member
 
Registered: Jul 2009
Posts: 262

Original Poster
Rep: Reputation: 2
Hi,
Sorry for not replying it earlier. But now I am going to use this forum again so i would avoid such abbreviations. Thanks for pointing out.

Zulfi.
 
Old 07-14-2014, 01:34 AM   #9
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,863
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
The beginning of the program could be like this:
Code:
/* programname.c */

#include <netdb.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>
(Sorry for the on-topic)
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
dereferencing pointer to incomplete type sweetymobil Programming 7 06-22-2009 07:46 AM
tai64nlocal.c:55: error: dereferencing pointer to incomplete type ExCIA Linux - General 1 03-31-2009 09:49 AM
error: dereferencing pointer to incomplete type ChullDouvre Programming 2 05-02-2007 12:16 AM
Error: dereferencing pointer to incomplete type cynthia_thomas Programming 1 05-01-2006 08:10 AM
C error "dereferencing pointer to incomplete type" lucs Slackware 6 02-21-2005 09:33 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:58 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration