Andre <> writes:
> I have a strange effect in a C compiler for a DSP processor ("VisualDSP").
>
> I want to pass a pointer to a short to a function, which is declared
> externally as short x[1000].
>
> So, what I do is:
>
> extern short *x;
>
> and later:
>
> short *my_function(short *a)
> {
> my code....
> }
<snip>
> The call would look like
> my_function(x);
>
> However, if I do my external declaration like
>
> extern short x[];
>
> everything works fine. No errors, no warnings in both cases.
>
> Question:
>
> What is the difference between
> extern short *x;
> and
> extern short[];
(you mean, presumably, extern short x[]
One is an array declaration and the other is a pointer declaration;
i.e. one is the truth and the other is a lie. Despite a lot of
misinformation pointers and arrays are not the same thing in C. While
it's true that in most contexts array names are converted to pointers,
the declaration has to be correct for that conversion to happen.
To get a low-level view of why it matters, think of what instructions
will be generated by x[0] = 1; within the scope of extern short *x; and
then within the scope of extern short x[];.
You get no errors or warnings because the type information has been lost
by the time the implementation gets round to matching up the incorrect
external pointer x with the actual array x. C is specified with this in
mind -- the language does not require a diagnostic for this error,
though a clever linker could certainly provide one.
--
Ben.