i have the structure hostent (built in)

struct hostent {
char *h_name;
char **h_aliases;
int h_addrtype;
int h_length;
char **h_addr_list;

h_name     Official name of the host.
h_aliases A NULL-terminated array of alternate names for the host.
h_addrtype The type of address being returned
h_length The length of the address in bytes.
h_addr_list A zero-terminated array of network addresses for the host

one of the built in methods (gethostbyname) returns a pointer to hostent

i need to print all the elements in the h_aliases , while the number of these elements is unknown

how could i do


You do as is implied in your question - you keep looping through the h_aliases array until you reach the end - or rather while you are not at the end:

       while current alias not at end of h_aliases array
         process current alias
         point to next alias

From the above we can  see we need a concept of the current alias, the next alias and the end of the array. In your case processing the current alias would be printing it out.

The end condition is a NULL char * value in the array. As for the current and next aliases, one common way is to just increment a copy of h_aliases  char * pointer:

       char ** current_alias_ptr = my_hostent_ptr->h_aliases;

       while ( *current_alias_ptr != NULL )
         ProcessAlias( *current_alias_ptr );

Remember that h_aliases is a pointer to something that is itself a pointer to char.  De-referencing the h_aliases value or the current_alias_ptr (which is the same type and starts by pointing to the same place) leaves us with a char * - another pointer.

Incrementing current_alias_ptr points us to the next slot in the array of char pointers - which will contain a NULL pointer value if the end has been reached. Notice that testing at the top of the loop handles the case where there are no aliases and the h_aliases member contains a pointer to a NULL char * value to start with.

An alternate way would be to use an integer index value:

       unsigned int current_alias_idx = 0;

       while ( *(my_hostent_ptr->h_aliases + current_alias_idx)
         != NULL
         ProcessAlias( *( my_hostent_ptr->h_aliases
         + current_alias_idx

Which as you can see is more long winded and in fact requires more work to constantly add the index to the array start address. However, it is equivalent to the following version using array element access:

       unsigned int current_alias_idx = 0;

       while ( my_hostent_ptr->h_aliases[current_alias_idx]!= NULL )
         ProcessAlias( my_hostent_ptr->h_aliases

Note that I have purposely wrapped long lines for a better code layout than the default wrapping in my text editor. I have also stuck with C style syntax rather than C++ for things such as variable initialisation - however the code will (should) compile and run using C++ (in fact I checked using 2 C++ compilers).

Hope this helps.


All Answers

Answers by Expert:

Ask Experts


Ralph McArdell


I am a software developer with more than 15 years C++ experience and over 25 years experience developing a wide variety of applications for Windows NT/2000/XP, UNIX, Linux and other platforms. I can help with basic to advanced C++, C (although I do not write just-C much if at all these days so maybe ask in the C section about purely C matters), software development and many platform specific and system development problems.


My career started in the mid 1980s working as a batch process operator for the now defunct Inner London Education Authority, working on Prime mini computers. I then moved into the role of Programmer / Analyst, also on the Primes, then into technical support and finally into the micro computing section, using a variety of 16 and 8 bit machines. Following the demise of the ILEA I worked for a small company, now gone, called Hodos. I worked on a part task train simulator using C and the Intel DVI (Digital Video Interactive) - the hardware based predecessor to Indeo. Other projects included a CGI based train simulator (different goals to the first), and various other projects in C and Visual Basic (er, version 1 that is). When Hodos went into receivership I went freelance and finally managed to start working in C++. I initially had contracts working on train simulators (surprise) and multimedia - I worked on many of the Dorling Kindersley CD-ROM titles and wrote the screensaver games for the Wallace and Gromit Cracking Animator CD. My more recent contracts have been more traditionally IT based, working predominately in C++ on MS Windows NT, 2000. XP, Linux and UN*X. These projects have had wide ranging additional skill sets including system analysis and design, databases and SQL in various guises, C#, client server and remoting, cross porting applications between platforms and various client development processes. I have an interest in the development of the C++ core language and libraries and try to keep up with at least some of the papers on the ISO C++ Standard Committee site at http://www.open-std.org/jtc1/sc22/wg21/.


©2017 About.com. All rights reserved.

[an error occurred while processing this directive]