* Query: Declaring arrays as extern
@ 2011-12-22 9:04 viresh kumar
2011-12-22 9:04 ` viresh kumar
2011-12-23 18:27 ` Alok Singhal
0 siblings, 2 replies; 4+ messages in thread
From: viresh kumar @ 2011-12-22 9:04 UTC (permalink / raw)
To: linux-c-programming
Cc: Shiraz HASHIM, pratyush.anand, vipin.kumar, Viresh KUMAR,
Vipul Kumar SAMAR, bhavna.yadav, Deepak SIKRI, Armando VISCONTI,
Bhupesh SHARMA, rajeev, Amit VIRDI
Hi,
I can observing an behavior which i am not able to explain.
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.
I know we pass array addresses to routines this way only, but with
extern it is just not working.
Can somebody please explain it?
--
viresh
^ permalink raw reply [flat|nested] 4+ messages in thread* Re: Query: Declaring arrays as extern
2011-12-22 9:04 Query: Declaring arrays as extern viresh kumar
@ 2011-12-22 9:04 ` viresh kumar
2011-12-23 16:38 ` subin gangadharan
2011-12-23 18:27 ` Alok Singhal
1 sibling, 1 reply; 4+ messages in thread
From: viresh kumar @ 2011-12-22 9:04 UTC (permalink / raw)
To: linux-c-programming
Cc: Shiraz HASHIM, pratyush.anand, vipin.kumar, Viresh KUMAR,
Vipul Kumar SAMAR, bhavna.yadav, Deepak SIKRI, Armando VISCONTI,
Bhupesh SHARMA, rajeev, Amit VIRDI
On Thu, Dec 22, 2011 at 2:34 PM, viresh kumar <viresh.linux@gmail.com> wrote:
> Hi,
>
> I can observing an behavior which i am not able to explain.
I am
sorry. :)
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: Query: Declaring arrays as extern
2011-12-22 9:04 ` viresh kumar
@ 2011-12-23 16:38 ` subin gangadharan
0 siblings, 0 replies; 4+ messages in thread
From: subin gangadharan @ 2011-12-23 16:38 UTC (permalink / raw)
To: viresh kumar
Cc: linux-c-programming, Shiraz HASHIM, pratyush.anand, vipin.kumar,
Viresh KUMAR, Vipul Kumar SAMAR, bhavna.yadav, Deepak SIKRI,
Armando VISCONTI, Bhupesh SHARMA, rajeev, Amit VIRDI
Hi Viresh,
I think this is what's happening in your case.
Imagine array address is 0x1000 and the contents stored are 1,2,3,4...10.
So when you access it as a pointer int *array,address of array will
get 0x1000 and array variable will have the
value 1.So when you print it the value will be 1 and when you
dereference it,it will try to fetch from 0x1.
On Thu, Dec 22, 2011 at 3:04 AM, viresh kumar <viresh.linux@gmail.com> wrote:
> On Thu, Dec 22, 2011 at 2:34 PM, viresh kumar <viresh.linux@gmail.com> wrote:
>> Hi,
>>
>> I can observing an behavior which i am not able to explain.
>
> I am
>
> sorry. :)
> --
> To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at http://vger.kernel.org/majordomo-info.html
--
With Regards
Subin Gangadharan
I am not afraid and I am also not afraid of being afraid.
--
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: Query: Declaring arrays as extern
2011-12-22 9:04 Query: Declaring arrays as extern viresh kumar
2011-12-22 9:04 ` viresh kumar
@ 2011-12-23 18:27 ` Alok Singhal
1 sibling, 0 replies; 4+ messages in thread
From: Alok Singhal @ 2011-12-23 18:27 UTC (permalink / raw)
To: viresh kumar
Cc: linux-c-programming, Shiraz HASHIM, pratyush.anand, vipin.kumar,
Viresh KUMAR, Vipul Kumar SAMAR, bhavna.yadav, Deepak SIKRI,
Armando VISCONTI, Bhupesh SHARMA, rajeev, Amit VIRDI
On Thu, Dec 22, 2011 at 1:04 AM, viresh kumar <viresh.linux@gmail.com> 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@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2011-12-23 18:27 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2011-12-22 9:04 Query: Declaring arrays as extern viresh kumar
2011-12-22 9:04 ` viresh kumar
2011-12-23 16:38 ` subin gangadharan
2011-12-23 18:27 ` Alok Singhal
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).