arrays

S

sangeeta chowdhary

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?
 
K

Keith Thompson

Joe Wright said:
sangeeta said:
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.
 
A

achp

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.
 
R

Richard Tobin

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
 
A

achp

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. :)
 
S

simn_stv

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

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!)
 
B

Barry Schwarz

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,161
Messages
2,570,892
Members
47,430
Latest member
7dog123

Latest Threads

Top