446,194 Members | 838 Online
Need help? Post your question and get tips & solutions from a community of 446,194 IT Pros & Developers. It's quick & easy.

 P: n/a int main() { int v1[10],v2[10]; int i1 = &v1[5] - &v1[3]; cout<<"i1:--"<
5 Replies

 P: n/a "yashwant pinge"

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

 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"

 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 byteaddresses, 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 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.