Difference of extern short *x and extern short x[]?

A

Andre

Hi all,

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....
}


If I call that function, correctly prototyped, the argument that ends up
in the function is not the pointer, but the value where it points to.

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[];

Best regards,

Andre
 
J

jacob navia

When you write:

extern short *x;

You declare that there is a zone of a few bytes holding a pointer to
short data somewhere in another module of your program.

Specifically that memory area is sizeof(short *) bytes long.

When you write:

extern short x[];

You are declaring that somewhere else there is an area of short data of
undeterminate length. That length is some multiple of sizeof(short) from
one to "many". There are no pointers in this declaration.

When you call a function, following the language rules, an array in this
case an array of shorts "decays" into a pointer into the first element
so inside the function you get a pointer to the first element
of the array.

Easy.
 
B

Ben Bacarisse

Andre said:
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....
}
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.
 
A

Andre

OK, thanks a bunch.

I guess from the practice to use something declared as an array as a
pointer, I lost the difference of the two or assumed C would not make any...

Thanks again,

Andre


Andre said:
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....
}
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.
 
K

Keith Thompson

Andre said:
I guess from the practice to use something declared as an array as a
pointer, I lost the difference of the two or assumed C would not make any...
[...]

Read section 6 of the comp.lang.c FAQ, <http://www.c-faq.com/>.

Then read the rest of it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,954
Messages
2,570,116
Members
46,704
Latest member
BernadineF

Latest Threads

Top