C Basic
-
-
I am not able to understand the output of below lines. As per my understanding output should be 8 because we are subtracting int address which is having 8 byte diffrence but output is coming 2. Please expalin.
int a[]={5,10,15,20};
int d = &a[2]-&a[0]; -
I am not able to understand the output of below lines. As per my understanding output should be 8 because we are subtracting int address which is having 8 byte diffrence but output is coming 2. Please expalin.
int a[]={5,10,15,20};
int d = &a[2]-&a[0];See this;
&a[2] - &a[0]
(a + 2) - (a + 0)
a + 2 - a - 0
2 - 0
2the above is true because use pointers you will always get the difference of elements
You talk about Being HUMAN. I have it in my name AnsHUMAN
-
See this;
&a[2] - &a[0]
(a + 2) - (a + 0)
a + 2 - a - 0
2 - 0
2the above is true because use pointers you will always get the difference of elements
You talk about Being HUMAN. I have it in my name AnsHUMAN
_AnsHUMAN_ wrote:
a + 2 - a - 0 2 - 0
While your end result is correct, shouldn't that be: a + 2 - a + 0 2 + 0
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Show me a community that obeys the Ten Commandments and I'll show you a less crowded prison system." - Anonymous
-
I am not able to understand the output of below lines. As per my understanding output should be 8 because we are subtracting int address which is having 8 byte diffrence but output is coming 2. Please expalin.
int a[]={5,10,15,20};
int d = &a[2]-&a[0];If the address of (the first item in)
a
is, for example, 0x1000 and each item ina
is 4 bytes, then the difference between the third item (0x1008) and the first item would be 8 divided by the size of anint
(most likely 4), thus 2 would be the output."One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Show me a community that obeys the Ten Commandments and I'll show you a less crowded prison system." - Anonymous
-
I am not able to understand the output of below lines. As per my understanding output should be 8 because we are subtracting int address which is having 8 byte diffrence but output is coming 2. Please expalin.
int a[]={5,10,15,20};
int d = &a[2]-&a[0];You are correct, the differene in the addresses is 8. I can only assume the compiler is dividing that by the type of dm which being an int, makes the result 2. Did you try it with char d = &a[2] - &a[0] ? It wold be interesting. IMO the compiler shouldnt do this, it is unexpected. I would expect to get 8 back, after all, that is the difference in the pointers.
============================== Nothing to say.
-
I am not able to understand the output of below lines. As per my understanding output should be 8 because we are subtracting int address which is having 8 byte diffrence but output is coming 2. Please expalin.
int a[]={5,10,15,20};
int d = &a[2]-&a[0]; -
You are correct, the differene in the addresses is 8. I can only assume the compiler is dividing that by the type of dm which being an int, makes the result 2. Did you try it with char d = &a[2] - &a[0] ? It wold be interesting. IMO the compiler shouldnt do this, it is unexpected. I would expect to get 8 back, after all, that is the difference in the pointers.
============================== Nothing to say.
-
You are correct, the differene in the addresses is 8. I can only assume the compiler is dividing that by the type of dm which being an int, makes the result 2. Did you try it with char d = &a[2] - &a[0] ? It wold be interesting. IMO the compiler shouldnt do this, it is unexpected. I would expect to get 8 back, after all, that is the difference in the pointers.
============================== Nothing to say.
Erudite_Eric wrote:
You are correct, the differene in the addresses is 8.
On the contrary he is totally wrong. The difference is the count of elements in the array, between the first and last item. This is C language, not machine code, as designed by Kernighan & Ritchie.
Use the best guess
-
I am not able to understand the output of below lines. As per my understanding output should be 8 because we are subtracting int address which is having 8 byte diffrence but output is coming 2. Please expalin.
int a[]={5,10,15,20};
int d = &a[2]-&a[0];You can subtract two pointers of the same base type, and the result is the number of elements of the base type that fit between the addresses which the pointers hold. Likewise, you can add an integer value to a pointer, and that will result in the address of a data type instance so and so many steps above the original pointer. Confusing? - Take your example:
int a[]={5,10,15,20};
Assume
a
starts at address 0x00040000. This is also the address ofa[0]
. In C,a
is the same as&a[0]
.a+1
is&a[1]
or 0x00040004, anda+2
is&a[2]
or 0x00040008.&a[2]-2
is&a[0]
or 0x00040000. Whenx-y==z
is true, thenx-z==y
should also be true; when&a[2]-2==&a[0]
is true, then&a[2]-&a[0]==2
should also be true. The C language is nice enough to fulfil this. To calculate the number of bytes between two memory locations, convert the pointers to BYTE* like this:(BYTE*)&a[2]-(BYTE*)&a[0]
This would evaluate to 8, as you expected. Assuming, of course, that 'int' is a 32-bit integer type.