473,385 Members | 1,867 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

struct hostent C

I need to understand this point, so perhaps someone would be able to help. The following code:

Expand|Select|Wrap|Line Numbers
  1. struct hostent {
  2. char*    h_name;       
  3. char**   h_aliases;    
  4.  int      h_addrtype;   ;; host address type 
  5. int      h_length;     ;; length of address 
  6. char**   h_addr_list;  ;; list of addresses 
  7. }
  8.  
is part of the C Library. Of this, I am interested in knowing
char** h_aliases;points to an array of strings and uses NULL (or 0) as the last element in the list. If you put, -1 in place of NULL, you get a compiler warning. So my question is, why is the compiler willing to accept an array initialization in which the last element is not a string, but an integer?


Thanks a lot.
Jul 10 '08 #1
6 25832
Banfa
9,065 Expert Mod 8TB
Of this, I am interested in knowing
char** h_aliases;points to an array of strings and uses NULL (or 0) as the last element in the list.
No it is not an array of strings, it is an array of pointers (which point to strings). A pointer can be NULL (that is not point to anywhere).

The actual value of the NULL pointer is platform dependent however the C/C++ standards state that the value 0 used in a pointer context is converted to the NULL pointer value for the target platform.

So you are not assigning an integer to a string, you are assigning an integer to a pointer and when you use 0, which the compiler knows is a place holder for the NULL pointer, it is valid but if you use any other integer value, say -1, the compiler does not know how to convert this to a pointer and you get a warning.


P.S. struct hostent is not part of the C library, it is part of the socket library.
Jul 10 '08 #2
No it is not an array of strings, it is an array of pointers (which point to strings). A pointer can be NULL (that is not point to anywhere).

The actual value of the NULL pointer is platform dependent however the C/C++ standards state that the value 0 used in a pointer context is converted to the NULL pointer value for the target platform.

So you are not assigning an integer to a string, you are assigning an integer to a pointer and when you use 0, which the compiler knows is a place holder for the NULL pointer, it is valid but if you use any other integer value, say -1, the compiler does not know how to convert this to a pointer and you get a warning.


P.S. struct hostent is not part of the C library, it is part of the socket library.
Thanks, but why is the last argument an integer and not a string?
Jul 10 '08 #3
JosAH
11,448 Expert 8TB
Thanks, but why is the last argument an integer and not a string?
It's just a matter of definition; this is what the C99 Standard has to say about it:


[#3] An integer constant expression with the value 0, or
such an expression cast to type void *, is called a null
pointer constant.48) If a null pointer constant is |
converted to a pointer type, the resulting pointer, called a
null pointer, is guaranteed to compare unequal to a pointer
to any object or function.

[#4] Conversion of a null pointer to another pointer type
yields a null pointer of that type. Any two null pointers
shall compare equal.

[#5] An integer may be converted to any pointer type. |
Except as previously specified, the result is
implementation-defined, might not be properly aligned, and
might not point to an entity of the referenced type.49)
kind regards,

Jos
Jul 10 '08 #4
It's just a matter of definition; this is what the C99 Standard has to say about it:



kind regards,

Jos
So, can we say that char** h_aliases is an integer pointer and which is why we use 0 as the last element denoting a null pointer?
Jul 10 '08 #5
Banfa
9,065 Expert Mod 8TB
So, can we say that char** h_aliases is an integer pointer and which is why we use 0 as the last element denoting a null pointer?
No, char **h_aliases is a pointer to a pointer to a char. Since it is a pointer you can use 0 as the last element denoting a null pointer. integers have nothing to do with it.
Jul 10 '08 #6
No, char **h_aliases is a pointer to a pointer to a char. Since it is a pointer you can use 0 as the last element denoting a null pointer. integers have nothing to do with it.
Thank you very much for clarifying.
Jul 10 '08 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Roy Hills | last post by:
When I'm reading from or writing to a network socket, I want to use a struct to represent the structured data, but must use an unsigned char buffer for the call to sendto() or recvfrom(). I have...
5
by: PCHOME | last post by:
Hello! I am working on dividing a single C file into several files. Now I encounter a problem about the global variables and can not find a way to solve it. All global variables and codes used...
19
by: Russell Shaw | last post by:
Hi, I have two structs in a header file, and they reference each other, causing a compile error. Is there a standard way to deal with this? typedef struct { ... RtAction *actions; }...
16
by: burn | last post by:
Hello, i am writing a program under linux in c and compile my code with make and gcc. Now i have 4 files: init.c/h and packets.c/h. Each header-file contains some: init.h: struct xyz {
5
by: Johs32 | last post by:
I have a struct "my_struct" and a function that as argument takes a pointer to this struct: struct my_struct{ struct my_struct *new; }; void my_func(struct my_struct *new); I have read...
7
by: Alex | last post by:
If I have two struct. See below: struct s1 { int type; int (*destroy)(struct s1* p); } struct s2 { struct s1 base;
4
by: hobbes992 | last post by:
Howdy folks, I've been working on a c project, compiling using gcc, and I've reached a problem. The assignment requires creation of a two-level directory file system. No files have to be added or...
4
by: hugo.arregui | last post by:
Hi! I have two struts like that: struct { int num; int num2; struct b arrayOfB; } a;
4
by: Sheldon | last post by:
Hi, I have a unique case where I need an array of structs that grows and within this array is another struct that grows in some cases. I'm having trouble allocating memory. Since I have never...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.