DSF
4/28/2011 12:20:00 AM
On 27 Apr 2011 07:58:33 GMT, jt@toerring.de (Jens Thoms Toerring)
wrote:
>DSF <notavalid@address.here> wrote:
>> On 27 Apr 2011 00:10:10 GMT, jt@toerring.de (Jens Thoms Toerring)
>>
>> >> r = foo((const **)c_array);
>> >
>> >I don't think the compiler will really like this (it could
>> >be upset about the missing type and maybe mumble something
>> >about using 'int' as a default)...
>
>> What missing type? The above line is what's necessary for the
>> compiler to not complain.
>
>The '(const **)' bit is missing the type to cast to, it
>just says 'cast to pointer to const pointer of something
>of uspecified type' and I guess you just missed the 'char'
>bit in there. At least my compiler doesn't find that just
>right and tells me it's using 'int' as the default type.
Yup! My typo. It was supposed to be
r = foo((const char **)c_array);
And I missed it twice!
{snipped}
>So you make 'a' indirectly point to an (of course im-
>mutable) string literal in foo() even though it's not
>declared as a pointer to const char. And now there's
>no protection anymore against changing what 'a' points
>to in main(). But when you now try to modify it things
>can go badly wrong. That should be excluded by using
>
>void foo( const char * const * p );
>
>since with that what 'p' points to also can't modified
>in foo() (although my compiler still complains and I
>haven't yet figured out why - or what what nasty things
>I still could do in foo() ;-)
Mine is content and doesn't give a peep with:
char *StringArrayToMultistring(const char * const *stringarray, char
*multistring);
....
char **s_array, ms[200];
....
(Allocates and fills s_array)
....
StringArrayToMultistring(s_array, ms);
To use a real world example.
Here's the code for StringArrayToMultistring, for the heck of it. It
takes an array of character arrays and copies them in order, complete
with terminators, into a single character array terminated by an empty
string (a single 0).
char *StringArrayToMultistring(const char * const *stringarray, char
*multistring)
{
size_t sai, msi, asl;
sai = msi = 0;
while(stringarray[sai] != NULL)
{
asl = strlen(stringarray[sai]) + 1;
memcpy(multistring + msi, stringarray[sai], asl);
msi += asl;
sai++;
}
multistring[msi] = 0;
return multistring;
}
DSF