- Subject: Re: Query: Declaring arrays as extern
- From: Alok Singhal <as8ca@xxxxxxxxxxxx>
- Date: Fri, 23 Dec 2011 10:27:15 -0800
- Cc: linux-c-programming@xxxxxxxxxxxxxxx, Shiraz HASHIM <shiraz.hashim@xxxxxx>, "pratyush.anand" <pratyush.anand@xxxxxx>, "vipin.kumar" <vipin.kumar@xxxxxx>, Viresh KUMAR <viresh.kumar@xxxxxx>, Vipul Kumar SAMAR <vipulkumar.samar@xxxxxx>, bhavna.yadav@xxxxxx, Deepak SIKRI <deepak.sikri@xxxxxx>, Armando VISCONTI <armando.visconti@xxxxxx>, Bhupesh SHARMA <bhupesh.sharma@xxxxxx>, rajeev <rajeev-dlh.kumar@xxxxxx>, Amit VIRDI <Amit.VIRDI@xxxxxx>
- In-reply-to: <CAOh2x=ktZBLD2KDOxdBGtz0Acqc0RDkA-VrCCCO2Nby8D=2FRg@mail.gmail.com>
- List-id: <linux-c-programming.vger.kernel.org>
- References: <CAOh2x=ktZBLD2KDOxdBGtz0Acqc0RDkA-VrCCCO2Nby8D=2FRg@mail.gmail.com>
On Thu, Dec 22, 2011 at 1:04 AM, viresh kumar <viresh.linux@xxxxxxxxx> wrote:
> Hi,
>
> file1.c
>
> /* global array */
> int arr[10];
>
> void main()
> {
> printf("addres of array is %p", arr);
> }
>
>
> file2.c
> extern int arr[];
>
> void main()
> {
> printf("addres of array is %p", arr);
> }
>
>
> file3.c
> extern int *arr;
>
> void main()
> {
> printf("addres of array is %p", arr);
> }
>
>
> Now, value printed with file 1 and 2 are same, but extern int *arr
> doesn't work at all. The address shown is just something else.
Presumably you are compiling file1.c without the definition of "main"
in when you compile file2.c and file3.c with it, and with the
definition of main when you are compiling file1.c as a standalone
unit.
This is happening because an array is not a pointer. In fact, your
question is so frequently asked, that it is on comp.lang.c FAQ list:
http://c-faq.com/aryptr/aryptr1.html
You might also want to read all the questions and answers in
http://c-faq.com/aryptr/index.html.
> I know we pass array addresses to routines this way only, but with
> extern it is just not working.
When you pass an array to a function, the array name is converted to a
pointer to its first element. But that is not what is happening in
your code above: "extern int *arr;" is not the right declaration for
'arr'.
Also, you should be saying int main(void) instead of void main().
--
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
[Linux Assembler]
[Git]
[Kernel List]
[Fedora Development]
[Fedora Announce]
[Autoconf]
[Yosemite Campsites]
[Yosemite News]
[GCC Help]