Shao Miller
5/17/2011 5:44:00 PM
On 5/17/2011 04:21, janus wrote:
> On Monday, May 16, 2011 3:25:35 PM UTC+1, Shao Miller wrote:
>> On 5/16/2011 07:05, janus wrote:
>>>> Shao Miller
>>>> /* Join non-empty strings with a specified, non-empty delimiter string */
>>>> int join_strings_with_delim(
>>>> struct string * output_str,
>>>> const struct string * const * input_strings,
>>>> const unsigned int input_string_count,
>>>> const struct string * delim,
>>>> int allocation
>>>> ) {
>>>> int i;
>>>> size_t output_size;
>>>> char * buf;
>>>>
>>>> /* Check output string */
>>>> ...
>>>
>>> I am confuse... Could explain the following;
>>>
>>> const struct string * const * input_strings, # Having two consts and pointer symbol staggered
>>
>> Absolutely, I'm happy to explain. :)
>>
>> 'input_strings' is a pointer ('*') to a 'const'-qualified pointer ('*')
>> to a 'const'-qualified 'struct string'.
>>
>> ( [ (const struct string) * const] * input_strings)
>>
>> What does this madness mean? It means:
>>
>> - 'input_strings' is not 'const'-qualified, so it can be changed. For
>> example, you could do:
>>
>> input_strings++;
>>
>> - 'input_strings' points to a pointer. _That_ pointer is
>> 'const'-qualified, so it mustn't be changed. For example, you could not do:
>>
>> input_strings[0] = ...
>> *input_strings = ...
>>
>> - 'input_strings' points to a pointer, and _that_ pointer points to a
>> 'struct string'. That 'struct string' is 'const'-qualified, so it
>> mustn't be changed. For example, you could not do:
>>
>> input_strings[0][0].len = ...
>> (*input_strings)[0].len = ...
>> (*input_strings)->len = ...
>> (*(*input_strings)).len = ...
>
> Let me explain what I have picked all this while.
> "const struct string * const * input_strings" is a two dimensional array.Which I could re-write as
> const struct string const input_strings[n][m]. input_strings[i] may be updated, however input_strings[i][k] will not update. And input_strings[i][k].len will not update.
Well, not exactly. As my understanding goes, an array, no matter how
many dimensions it has, implies a contiguous range of elements of the
base-level element type. For example:
int main(void) {
static const char ca[3][5] = {
{'0', '1', '2', '3', '4'},
{'5', '6', '7', '8', '9'},
{'A', 'B', 'C', 'D', 'E'},
};
return 0;
}
The above multi-dimensional array's characters, in memory, would
resemble the characters in:
char ca_bytes[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'A', 'B', 'C', 'D', 'E'};
However in the code sample you are posting in response to, there is a
one-dimensional array of 'struct string' for "New York", "New Jersey",
"New London". Then there is a _separate_ array of _pointers_ where each
element (a pointer to 'struct string') _points_ to an element of the
first array. The two arrays are separate, and even have different base
element types: 'struct string' for the first, but 'struct string *' for
the second.
The 'join_strings_with_delim()' function takes an array of 'struct
string *' rather than an array of 'struct string'. The 'const'
qualifiers mean "I shall not modify the array of pointers, nor shall I
modify the pointed-to structures." Since there are two subjects that
are not modified, that is why there are two 'const's in there.