Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

arrays

2 views
Skip to first unread message

sangeeta chowdhary

unread,
Feb 15, 2010, 4:28:39 PM2/15/10
to
if i have any integer array
int arr[10];
printf("%u%u",&arr,arr);

gives the same base address of array?
how is this possible?
how arr and &arr can give the same address?

Joe Wright

unread,
Feb 15, 2010, 4:47:23 PM2/15/10
to

Well, &arr is the address of the array and arr is the address of its first
element. The two expressions have the same value but different type.

Printing pointer values with %u is undefined.

--
Joe Wright
"If you rob Peter to pay Paul you can depend on the support of Paul."

Keith Thompson

unread,
Feb 15, 2010, 5:29:04 PM2/15/10
to
Joe Wright <joeww...@comcast.net> writes:
> sangeeta chowdhary wrote:
>> if i have any integer array
>> int arr[10];
>> printf("%u%u",&arr,arr);
>>
>> gives the same base address of array?
>> how is this possible?
>> how arr and &arr can give the same address?
>
> Well, &arr is the address of the array and arr is the address of its
> first element.

Yes, but more precisely, arr is an expression of array type that
refers to value of the the array object. But an expression of array
type, in most contexts, is implicitly converted to a pointer to the
array's first element.

> The two expressions have the same value but different
> type.

Right.

> Printing pointer values with %u is undefined.

Right.

This:

int arr[10];
printf("%p %p\n", (void*)&arr, (void*)arr);

will almost certainly print the same value twice.

--
Keith Thompson (The_Other_Keith) ks...@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

achp

unread,
Feb 16, 2010, 3:25:59 AM2/16/10
to
On 16 фев, 01:29, Keith Thompson <ks...@mib.org> wrote:
> This:
>
>     int arr[10];
>     printf("%p %p\n", (void*)&arr, (void*)arr);
>
> will almost certainly print the same value twice.

Why "almost"? I cannot see any reason for "almost". I believe it
*must* print the same value twice.

Richard Heathfield

unread,
Feb 16, 2010, 3:27:56 AM2/16/10
to

What is the basis of your belief?

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
"Usenet is a strange place" - dmr 29 July 1999
Sig line vacant - apply within

achp

unread,
Feb 16, 2010, 7:23:59 AM2/16/10
to
On 16 фев, 11:27, Richard Heathfield <r...@see.sig.invalid> wrote:
> What is the basis of your belief?

Well, I was sure there was something about type compatibility. But now
that I've examined the standard, I see I'm wrong.

Richard Tobin

unread,
Feb 16, 2010, 7:53:23 AM2/16/10
to
In article <5bff639c-1522-4e20...@36g2000yqu.googlegroups.com>,

achp <ach...@gmail.com> wrote:
>> int arr[10];
>> printf("%p %p\n", (void*)&arr, (void*)arr);
>>
>> will almost certainly print the same value twice.

>Why "almost"? I cannot see any reason for "almost". I believe it
>*must* print the same value twice.

Is it even true that

int x;
printf("%p %p\n", (void*)&x, (void*)&x);

must print the same value twice? As a trivial example, it could
print "0x0000123a" and "0x0000123A"; on a system with segmented
addresses it could print "0012:003a" and "0010:023a'.

-- Richard
--
Please remember to mention me / in tapes you leave behind.

achp

unread,
Feb 16, 2010, 10:33:33 AM2/16/10
to
On 16 фев, 15:53, rich...@cogsci.ed.ac.uk (Richard Tobin) wrote:
> Is it even true that
>
>    int x;
>    printf("%p %p\n", (void*)&x, (void*)&x);
>
> must print the same value twice?  As a trivial example, it could
> print "0x0000123a" and  "0x0000123A"; on a system with segmented
> addresses it could print "0012:003a" and "0010:023a'.

Well, this option is formally acceptable, too, though printing two
different strings for the same representation seems sort of
schizophrenic to me. :-)

achp

unread,
Feb 16, 2010, 10:34:44 AM2/16/10
to
On 16 фев, 18:33, achp <achp...@gmail.com> wrote:
> printing two
> different strings for the same representation

... or making two different representations for the same pointer
value ...

simn_stv

unread,
Feb 17, 2010, 3:37:30 AM2/17/10
to

it doesnt make much sense for it to print two different strings...ok
well, maybe its just me and my system (cos it prints the same value
twice!)

Barry Schwarz

unread,
Feb 17, 2010, 4:56:08 PM2/17/10
to
On Wed, 17 Feb 2010 00:37:30 -0800 (PST), simn_stv
<nan...@googlemail.com> wrote:

>On Feb 16, 4:34 pm, achp <achp...@gmail.com> wrote:

>> On 16 ???, 18:33, achp <achp...@gmail.com> wrote:
>>
>> > printing two
>> > different strings for the same representation
>>
>> ... or making two different representations for the same pointer
>> value ...
>>
>> > seems sort of
>> > schizophrenic to me. :-)
>
>it doesnt make much sense for it to print two different strings...ok
>well, maybe its just me and my system (cos it prints the same value
>twice!)

There is a difference between what happens on most (or even all)
current implementations and what the standard requires to happen. In
this case, the standard does not require the two outputs to be the
same. It's not that you (and I) can't see any reason for it to print
two different strings, it's that there is no reason in the standard
that it cannot.

There is also a difference between the limits of one's experience and
imagination and the real world. There are people who fervently
believe '0'-0x30 == 0, 'i'+1 == 'j', or an implementation of strlen or
strcpy requires the source code to have a loop looking at each
character in turn. (None of these are true on my system.) The fact
that I don't know of an implementation where sizeof(int) == 7 or
CHAR_BITS == 11 is irrelevant as far as the standard is concerned.

--
Remove del for email

0 new messages