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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
09-29-2010, 01:21 PM
|
#1
|
Member
Registered: Jul 2009
Posts: 262
Rep:
|
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 01:23 PM.
|
|
|
09-29-2010, 02:27 PM
|
#2
|
LQ Guru
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,352
|
Quote:
Originally Posted by zak100
plz
|
This writing style is not appropriate here.
|
|
|
09-29-2010, 02:42 PM
|
#3
|
LQ Guru
Registered: Dec 2007
Distribution: Centos
Posts: 5,286
|
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 02:55 PM.
|
|
|
09-29-2010, 03:08 PM
|
#4
|
Senior Member
Registered: May 2005
Posts: 4,481
|
Quote:
Originally Posted by johnsfine
...
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
.
|
|
|
09-29-2010, 03:50 PM
|
#5
|
Senior Member
Registered: May 2005
Posts: 4,481
|
Quote:
Originally Posted by johnsfine
...
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
.
|
|
|
09-30-2010, 01:21 AM
|
#6
|
Member
Registered: Jul 2009
Posts: 262
Original Poster
Rep:
|
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.
|
|
|
09-30-2010, 08:34 AM
|
#7
|
LQ Guru
Registered: Dec 2007
Distribution: Centos
Posts: 5,286
|
Quote:
Originally Posted by zak100
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).
|
|
|
07-14-2014, 01:40 AM
|
#8
|
Member
Registered: Jul 2009
Posts: 262
Original Poster
Rep:
|
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.
|
|
|
07-14-2014, 02:34 AM
|
#9
|
Senior Member
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,974
|
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)
|
|
|
All times are GMT -5. The time now is 08:51 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|