[A89] Re: c trouble


[Prev][Next][Index][Thread]

[A89] Re: c trouble



To the original problem, I use this to declare lists of strings.

char * string_list[]={"foo", "bar","etc",""}; //An array of pointers to 
strings.

string_list is an array of pointers, which passes just like a pointer to a 
pointer. Note that it ends in a null string. That way there is no need to 
pass the length of the list.

void iterate_list(char * string_list[])
{
  int x=0;
  while (string_list[x][0]!=0)
  {
    int y;
    for(y=0;string_list[x][y] !=0;y++) putchar(string_list[x][y]);
    putchar('\n');
	x++;
  }

}
Note that the for line could just as easily be
  for(y=0;y<strlen(string_list[x];y++) ,,,

Why I use arrays of pointers is this:
They work pretty much like pointers to pointers, but they are easier to 
declare. On most compilers, the following would produce an error:
char **srting_list{"foo","bar","etc",""};
because "foo" is a char[4], and "" is a char[1], but the compiler doesn't 
acknowledge that char[4] and char[1] are _functionally_ equivalent and so 
won't treat them as the same type. A little casting lets you fill char** 's 
at compile time but that involves too much typing for little 'ol me.


And yes, some of the comments are a little screwy, but not as invalid as 
they seem. Different perspectives is all. Don't get mad at people who try to 
help.



>From: Olof Hedman <alh@home.se>
>Reply-To: assembly-89@lists.ticalc.org
>To: assembly-89@lists.ticalc.org
>Subject: [A89] Re: c trouble
>Date: Wed, 03 Sep 2003 16:43:43 +0200
>
>JudeCN@aol.com wrote:
>
>>If you REALLY want to use **foo over foo[x][y], then assign foo an address 
>>where there will not be anything stored.  The easiest way is to use 
>>memset() to declare **foo as a buffer, or (less efficiently) use malloc() 
>>or calloc().  I still think that declaring foo[x][y] will be most 
>>effective; just make the x and y dimensions as big as the maximum number 
>>of terms could ever be stored in the array (determining this will be 
>>difficult; you must know EXACTLY how the code will behave when it stores 
>>pointers and characters into the array).
>>
>>Jude Nelson
>
>and this is just about as strange...
>Are we having a new strange variety of trolls here? :)
>
>//Olle
>
>

_________________________________________________________________
MSN 8: Get 6 months for $9.95/month. http://join.msn.com/?page=dept/dialup