473,387 Members | 1,512 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

address issue


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
5 2249
"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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Juha Suni | last post by:
I was faced with a difficult configuration issue a few days ago with another companys web service. In short, their web service requires the user to login on their page before their service can be...
7
by: Jay | last post by:
Hi, I am using the MailMessage object to send mails. One issue that I am currently facing is that the 'From' address that I provide is not getting resolved. I was testing the code with the same...
35
by: hasho | last post by:
Why is "call by address" faster than "call by value"?
29
by: chellappa | last post by:
hi all I need to write a program to find mac address of a my computer using libaries, is this possible? How? thanks
5
by: Kamaluokeakua | last post by:
I have to write an application that deals with clients in multiple countries. The addresses, phone numbers, country id and currency information has to be stored into a database that allows for the...
4
by: andreas.w.h.k. :-\) | last post by:
How do I change the address location in the wsdl <wsdl:port name="SearchSoap12" binding="tns:SearchSoap12"> <soap12:address location="http://searchservices/engine/search.asmx" /> </wsdl:port> ...
12
by: greg_chu | last post by:
Hi, I know I can use ipconfig.exe is DOS to get my IP address, the situation is I need to automat this process. I need to write a DOS program which issue a command to run IPCONFIG to get the IP...
8
by: msnews.microsoft.com | last post by:
Our IT team relocated our application servers yesterday. They had to take one machine out of the cluster because it would prompt users for credentials if accessed using the IP address, which I...
7
by: John Koleszar | last post by:
Hi all, I'm porting some code that provides compile-time assertions from one compiler to another and ran across what I believe to be compliant code that won't compile using the new compiler. Not...
10
by: themadjester | last post by:
This is weird, I know what an IP address conflict is, and how to avoid it, but this problem seems atypical - and apparently client side? Basically I have a router internet network, it is DHCP and...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.