By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,194 Members | 838 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,194 IT Pros & Developers. It's quick & easy.

address issue

P: n/a

int main()
{
int v1[10],v2[10];
int i1 = &v1[5] - &v1[3];
cout<<"i1:--"<<i1<<endl;
return 0;
}

The above snipet gives the value of i is 2, why it is?

what is the return type of &v1[5] , &v1[3] and (v1[5] - &v1[3])?

Yashwant Pinge

Mar 15 '07 #1
Share this Question
Share on Google+
5 Replies


P: n/a
"yashwant pinge" <ya***********@gmail.comwrote in message
news:11**********************@d57g2000hsg.googlegr oups.com...
:
: int main()
: {
: int v1[10],v2[10];
: int i1 = &v1[5] - &v1[3];
: cout<<"i1:--"<<i1<<endl;
: return 0;
: }
:
: The above snipet gives the value of i is 2, why it is?

Because C (and C++) perform typed pointer arithmetic.
2 is indeed the number of integers stored between
the two addresses.
What value would you expect ?

: what is the return type of &v1[5] , &v1[3] and (v1[5] - &v1[3])?

In the above, the return type of &v1[i] is int*. The difference
of two pointers returns an integer of type std::diff_t.

If you want to compute the actual difference between byte
addresses, you should cast the pointers to (unsigned char*):
int i2 = ((unsigned char*)&v1[5] - (unsigned char*)&v1[3]);
( on most 32-bit platforms, the value of i2 will be 8 )
( casting to char/signed char works too, but unsigned char
should be used if you want to access the representation
of the stored data... )

Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Brainbench MVP for C++ <http://www.brainbench.com

Mar 15 '07 #2

P: n/a
yashwant pinge wrote:
>
int main()
{
int v1[10],v2[10];
int i1 = &v1[5] - &v1[3];
cout<<"i1:--"<<i1<<endl;
return 0;
}

The above snipet gives the value of i is 2, why it is?
Because that's what the result should be according to the rules of pointer
arithmetic. What would you expect instead?
what is the return type of &v1[5] , &v1[3]
Pointer to int.
and (v1[5] - &v1[3])?
^
I guess you mean &v1[5]

The type is std::ptrdiff_t.

Mar 15 '07 #3

P: n/a
On Mar 15, 9:50 am, "Ivan Vecerina"
<_INVALID_use_webfo...@ivan.vecerina.comwrote:
If you want to compute the actual difference between byte
addresses, you should cast the pointers to (unsigned char*):
Or void*, but clearer IMHO would be to multiply by the size of one
element:

const std::diff_t byte_diff = (&v[5] - &v[3]) * sizeof v[0];

Cheers! --M

Mar 15 '07 #4

P: n/a
On 15 Mar, 14:22, "yashwant pinge" <yashwantpi...@gmail.comwrote:
int main()
{
int v1[10],v2[10];
int i1 = &v1[5] - &v1[3];
cout<<"i1:--"<<i1<<endl;
return 0;

}

The above snipet gives the value of i is 2, why it is?

what is the return type of &v1[5] , &v1[3] and (v1[5] - &v1[3])?
typeif(&v1[5]).name() gives me int* and typeid(&v1[5] - &v1[3]).name()
gives me int. So the first is a pointer to an int, which is quite
obvious and the second is an int and seems to represents the number of
times your have to run the ++-operator (or -- if negative) to get the
first equal to the second.

--
Erik Wikström

Mar 15 '07 #5

P: n/a
mlimber wrote:
On Mar 15, 9:50 am, "Ivan Vecerina"
<_INVALID_use_webfo...@ivan.vecerina.comwrote:
>If you want to compute the actual difference between byte
addresses, you should cast the pointers to (unsigned char*):

Or void*,
No. You can't do arithmetic on pointers to void, because void is an
incomplete type and doesn't have a size.
but clearer IMHO would be to multiply by the size of one
element:

const std::diff_t byte_diff = (&v[5] - &v[3]) * sizeof v[0];
I'd say the best would be to write a small template that does it, like:

template <typename T>
std::ptrdiff_t byte_offset(const T* p1, const T* p2)
{
return (p2 - p1) * sizeof(T);
}

const std::ptrdiff_t byte_diff = byte_offset(&v[3], &v[5]);

Mar 15 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.