Re: How to increment array of pointers to strings
- From: Keith Thompson <kst-u@xxxxxxx>
- Date: Fri, 06 Aug 2010 09:26:17 -0700
Shao Miller <sha0.miller@xxxxxxxxx> writes:
On Aug 5, 11:39 pm, Eric Sosman <esos...@xxxxxxxxxxxxxxxxxxxx> wrote:
On 8/5/2010 11:02 PM, Shao Miller wrote:Thanks, Eric. :) Let's change it to:
On Aug 5, 8:53 pm, barncat<thebarn...@xxxxxxxxx> wrote:
Thanks All! My brain hurts after reading and tyring to understandSomething else to make your brain hurt: An array has elements of a
your explanations. I think i get this now, or at least my program
works now :) Thanks again
fixed size. :) So you cannot have an array of strings of different
length.[...]
Actually, you can. Keeping in mind the definitions of "string"
and "string length,"
char array[][6] = { "forty", "two" };
assert (strlen(array[0]) != strlen(array[1])); // woops!
assert (sizeof(array[0]) == sizeof(array[1])); // granted ...
"Something else to make your brain hurt: An array has elements of a
fixed size. :) So you cannot have an array of arrays of 'char' with a
different number of elements for each sub-array."
Of course, one could argue that your array above is _more_ than an
array of strings, due to the padding after the second element. ;)
That is to say, if just one of the strings is long and the others
shorter, the entire array will have lots of padding, so one should be
aware of that.
Strictly speaking, there's no such thing as an "array of strings". A
string isn't a data type; it's a data format.
In the above, ``array'' is an array of arrays of char; each element is
of type char[6] and *contains* a string.
Consider:
char array[][6] = { "forty", "two" };
/* Is ``array'' an array of strings? */
memcpy(array[0], "abcdef", 6);
memcpy(array[1], "uvwxyz", 6);
/* It certainly isn't now. */
An array of foo is an array of foo regardless of its current contents.
For "foo" = "string", that doesn't work.
--
Keith Thompson (The_Other_Keith) kst-u@xxxxxxx <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
.
- References:
- How to increment array of pointers to strings
- From: barncat
- Re: How to increment array of pointers to strings
- From: Ben Bacarisse
- Re: How to increment array of pointers to strings
- From: barncat
- Re: How to increment array of pointers to strings
- From: Keith Thompson
- Re: How to increment array of pointers to strings
- From: Morris Keesan
- Re: How to increment array of pointers to strings
- From: Keith Thompson
- Re: How to increment array of pointers to strings
- From: barncat
- Re: How to increment array of pointers to strings
- From: Shao Miller
- Re: How to increment array of pointers to strings
- From: Eric Sosman
- Re: How to increment array of pointers to strings
- From: Shao Miller
- How to increment array of pointers to strings
- Prev by Date: Re: "claim", etc. (was Re: C Standard Regarding Null Pointer Dereferencing)
- Next by Date: Re: "claim", etc. (was Re: C Standard Regarding Null Pointer Dereferencing)
- Previous by thread: Re: How to increment array of pointers to strings
- Next by thread: Re: How to increment array of pointers to strings
- Index(es):
Relevant Pages
|