On 2 Jun 2004 14:32:10 -0700,
oldwolf@inspire.net.nz (Old Wolf) wrote:
[color=blue]
> Jacob Schmidt <Jacob.Schmidt@aol.com> wrote:[/color]
[color=blue][color=green]
> > const char message1[] = <snip> [message 2 similar]
> > char ( * const msgptr12 [] ) [] = { message1, message2 };[/color]
>[color=green]
> > /* lint gives a type mismatch error for the previous two lines. */[/color]
>
> 'msgptr12' is an array of const pointers to arrays (of unspecified size)
> of char.
> 'message1' is the name of an array of const char. This is not a
> pointer to (non-const) char. You need the '&' sign to take its
> address, and you need to make msgptr12 point to arrays of const char:
>
> const char (* const msgptr12[]) [] = { &message1, &message2 };
>[/color]
Agree so far.
[color=blue]
> This compiles OK now but it is not terribly useful.
> msgptr12[0] has type 'pointer to array (of unknown size) of const char'.
> This is an incomplete type, so you cannot actually dereference that
> pointer, so you can't access the chars in it safely.[/color]
To be clear, 'array of unknown size (of anything)' is the incomplete
type; 'pointer to X' is complete even if X is incomplete. But I think
you _can_ dereference it: this is a corner case, where it is allowed
to have a pointer to incomplete and dereference it to form an lvalue
of incomplete type; you explicitly musn't fetch (convert to rvalue) or
store (assign) such, but AFAICS if as here it is an array you can let
it decay to pointer to element and legally use that, within the bound
of the array object it points "to" (rather, to the base element of).
What you can't have is an actual (defined) object of incomplete type,
and in C99 you explicitly can't have a declaration for 'array of
incomplete' (which this example isn't, mind you) whereas in C89 _as a
function parameter_ an implementation _might_ let you declare array of
incomplete and "rewrite" it to pointer to incomplete which is OK.
[color=blue]
> The expression *msgptr12[0] would be a syntax error.
>[/color]
The standard doesn't define 'syntax error' but I think it is usually
taken to mean 'violation of any syntax rule', which this is not;
if anything it would be a constraint violation -- also a required
diagnostic, but a separate category. And I say not even that.
[color=blue][color=green]
> > printf ( "%s", msgptr12 [i] );[/color]
>
> Undefined behaviour - %s expects a pointer to char, but you gave it a
> pointer to array. You were (un)lucky that you chose a variadic function,
> if you tried:
> puts(msgptr12[i])
> you would get a compiler warning.
>[/color]
Agree here. In fact gcc will even unrequiredly warn on the printf.
[color=blue]
> If you want msgptr12's members to point to complete arrays, then the
> arrays all have to be the same size and you have to specify that, eg:
>
> const char (* const msgptr12[]) [40] = { &message1, &message2 };
> printf("%s", *msgptr12[1]);
>
> Or, as others have suggested, you could simply use an array of
> pointers to char (but this was a useful exercise in understanding
> pointers to arrays).[/color]
Yes.
- David.Thompson1 at worldnet.att.net