UNIX Socket FAQ

A forum for questions and answers about network programming on Linux and all other Unix-like systems

You are not logged in.

  • Index
  • » C
  • » List using pointers..

#1 2007-10-11 10:03 PM

abhishek_2301
Member
Registered: 2007-10-11
Posts: 3

Re: List using pointers..

Hi,

I have a sequential list of 4 characters which are to be accessed one after the another.How can I implement this using a format like "char **list".I dont want it to be a linked list.

Thank You!!!

Offline

#2 2007-10-11 11:40 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,839
Website

Re: List using pointers..

I'm not clear on the specifics...  You say 4 "characters", but then say "char **list",
which would make more sense to use for an array of strings (ie: pointers TO characters)...
Also, by "list" do you really mean a simple "array"?  That's what it sounds like you
want, but your terminology is confusing...

Anyway, if you have 4 separate chars which you want to make an array out of,
then there are multiple approaches...  The easiest probably being to just do
something like:

char arr[] = {CHAR1, CHAR2, CHAR3, CHAR4};
/* or, if the chars are not constant... */
char arr[4];
arr[0] = CHAR1;
arr[1] = CHAR2;
arr[2] = CHAR3;
arr[3] = CHAR4;

Or, if these 4 chars are in the form of a constant string, then you can just declare
a "char *" and point it at the constant...  (Also, if you need to treat them together as
a string, you'll want to add an extra space to the above array, if you use that
approach, so you can add a null char at the end...)

Or, is this something else entirely, which I'm not understanding??  It might help a
lot if you provide actual specific details (what these "characters" are exactly, and
what exactly you want to do with them in this "list")...

Offline

#3 2007-10-12 02:40 AM

abhishek_2301
Member
Registered: 2007-10-11
Posts: 3

Re: List using pointers..

Thxxs very much for your reply!!!
Actually I want to have a lost of hosts that I am passing as a struct member with the format of char **list to the hosts that are on the list.The first host receives it and then it forwards it to the next host and so on as sequentially defined in the list.
How will I be able to implement this concept!!!

Thanks and awaiting for your reply!!!

Offline

#4 2007-10-12 02:25 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,839
Website

Re: List using pointers..

I'm sorry, I still don't quite understand...  By "hosts", do you just mean hostnames as
strings?  Eg: "www.developerweb.net", "www.google.com", etc.?  Ok, assuming
that's so, then where are these hostnames coming from?  Are they some fixed,
constant list, hard-coded into your app, or coming from some dynamic source (a
config file, command-line, etc.)?  If the former, you can do much the same as I show
above with simple characters:

char *arr[] = {"www.developerweb.net", "www.google.com", "www.magrathea.com", "www.yahoo.com", NULL};

But, if they're not hard-coded constant values like that, you'll probably have to
dynamically allocate memory for them:

char **arr;
int i, num;
/* ...figure out correct "num" value for number of hostnames you're dealing with... */
arr = calloc (num + 1, sizeof (char *));
if (arr == NULL) die();
for (i = 0; i < num; i++) {
    /* ...read "hostname" from wherever they're coming from... */
    arr[i] = strdup (hostname);
    if (arr[i] == NULL) die();
}

Or, something like that...  It all depends on specific details of exactly what you're
doing...

Offline

  • Index
  • » C
  • » List using pointers..

Board footer

Powered by FluxBB