Re: Query: Declaring arrays as extern

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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


[Index of Archives]     [Linux Assembler]     [Git]     [Kernel List]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [C Programming]     [Yosemite Campsites]     [Yosemite News]     [GCC Help]

  Powered by Linux