LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 02-26-2004, 07:09 AM   #1
beginner16
Member
 
Registered: Nov 2003
Posts: 33

Rep: Reputation: 15
Very strange syntax in C and it works-why?


hi

gethostbyname() returns a structure and member of this structure is of type char **h_addr_list.So basicly h_add_list should point to an array of pointers to char.
Instead it points to an aray of pointers to struct in_addr.
How can this even work and why didn't the designers made h_addr_list of type struct in_addr **h_addr_list?

thanx
 
Old 02-26-2004, 07:24 AM   #2
eshwar_ind
Member
 
Registered: Feb 2004
Location: Bangalore
Distribution: Redhat
Posts: 144

Rep: Reputation: 15
I think struct in_addr is used to hold the IP address.
U pass ip address as a string . Is it right?
If its correct , They used char ** to support for maintanance (or portability) of previous code, because this IP address size (IPv6 new address) will change as the network grows. So they might have used it instead of that struct. When we use char **. we can get the full address regardless of its size(taking \0 as the last character).

I know very little. it may be correct or wrong. Dont scold me if its wrong.
bye,
Eshwar
 
Old 02-26-2004, 07:31 AM   #3
kev82
Senior Member
 
Registered: Apr 2003
Location: Lancaster, England
Distribution: Debian Etch, OS X 10.4
Posts: 1,263

Rep: Reputation: 50
if you look at the definition of struct in_addr (from netinet/in.h) you will see that the struct only has 1 member which is a 32bit unsigned integer which could be treated as 4 bytes or chars. so thats why it works, as for why they did it - i dont know as my network programming knowledge is quite limited.
 
Old 02-26-2004, 08:49 AM   #4
beginner16
Member
 
Registered: Nov 2003
Posts: 33

Original Poster
Rep: Reputation: 15
I understand,but still h_addr_list points to an array of pointers to structures and when I tried to apply similar syntax in my code(char pointer pointing to a structure of some sort) I got an error.
 
Old 02-26-2004, 01:54 PM   #5
kev82
Senior Member
 
Registered: Apr 2003
Location: Lancaster, England
Distribution: Debian Etch, OS X 10.4
Posts: 1,263

Rep: Reputation: 50
heres an example of treating an array of structs as an array of floats and it compiles/runs fine for me. hopefully this explains what happens.

Code:
#include <stdio.h>

typedef struct {
	float x;
	float y;
	float z;
} coord;

coord c[]={{-0.5f,-0.5f,0.0f},{0.5f,-0.5f,0.0f},{0.0f,1.0f,0.0f}};

int main() {
	int i;

	for(i=0;i<10;i++) {
		printf("%f\n", *(i+(float *)c));
	}
	return 0;
}
 
Old 02-26-2004, 05:55 PM   #6
beginner16
Member
 
Registered: Nov 2003
Posts: 33

Original Poster
Rep: Reputation: 15
hi

True,but you can't make a float array and store coord structures inside of it,which is basicly what char **h_addr_list does

thanx for an example ,I didn't think it was allowed
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
[C] Strange syntax error. darklogik_org Programming 2 01-04-2005 04:41 PM
Strange USB problems - it works AND it works not Nightfrost Linux - Hardware 2 10-07-2004 07:07 PM
strange error, cant login but su - works ! qwijibow Linux - Software 1 08-23-2004 10:47 AM
Wi-Fi works, but it's strange thejoe526 Linux - Wireless Networking 4 06-16-2004 03:44 PM
CD-R(W) works but strange errors Kristian2 Slackware - Installation 1 06-05-2004 02:54 PM


All times are GMT -5. The time now is 10:42 PM.

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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration