Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Difference of extern short *x and extern short x[]?

Reply
Thread Tools

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

 
 
Andre
Guest
Posts: n/a
 
      07-17-2012
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
 
Reply With Quote
 
 
 
 
jacob navia
Guest
Posts: n/a
 
      07-17-2012
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.


 
Reply With Quote
 
 
 
 
Ben Bacarisse
Guest
Posts: n/a
 
      07-17-2012
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.
 
Reply With Quote
 
Andre
Guest
Posts: n/a
 
      07-17-2012
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


On 17.07.2012 14:31, Ben Bacarisse wrote:
> 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.
>



 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      07-17-2012
On 7/17/2012 8:10 AM, Andre wrote:
>[...]
> What is the difference between
> extern short *x;
> and
> extern short[];


This is Question 6.1 on the comp.lang.c Frequently Asked
Questions (FAQ) page, <http://www.c-faq.com/>. FAQ 6.2 -- in
fact, much of Section 6 -- may also be helpful.

--
Eric Sosman
d


 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      07-17-2012
Andre <> writes:
[...]
> 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.

--
Keith Thompson (The_Other_Keith) kst- <http://www.ghoti.net/~kst>
Will write code for food.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
unsigned short, short literals Ioannis Vranos C Programming 5 03-05-2008 01:25 AM
longs, long longs, short short long ints . . . huh?! David Geering C Programming 15 01-11-2007 09:39 PM
unsigned short short? slougheed@gmail.com C++ 4 10-16-2006 11:25 PM
Difference between bin and obj directories and difference between project references and dll references jakk ASP .Net 4 03-22-2005 09:23 PM
extern const char * vs. extern const char []http://tinyurl.com/47e3k Thomas Matthews C++ 5 08-02-2004 10:36 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57