Reputation: 1419
Difference of two pointers of the same type is always one.
#include<stdio.h>
#include<string.h>
int main(){
int a = 5,b = 10,c;
int *p = &a,*q = &b;
c = p - q;
printf("%d" , c);
return 0;
}
Output is 1.
I dont get the reasoning behind it
Upvotes: 2
Views: 2929
Reputation: 263217
The behavior is undefined.
C99 6.5.6 paragraph 9 says:
When two pointers are subtracted, both shall point to elements of the same array object, or one past the last element of the array object; the result is the difference of the subscripts of the two array elements.
Paragraph 7 in the same section says:
For the purposes of these operators, a pointer to an object that is not an element of an array behaves the same as a pointer to the first element of an array of length one with the type of the object as its element type.
Section 4 paragraph 2 says:
If a "shall" or "shall not" requirement that appears outside of a constraint is violated, the behavior is undefined. Undefined behavior is otherwise indicated in this International Standard by the words "undefined behavior" or by the omission of any explicit definition of behavior. There is no difference in emphasis among these three; they all describe "behavior that is undefined".
3.4.3 defines the term "undefined behavior" as:
behavior, upon use of a nonportable or erroneous program construct or of erroneous data, for which this International imposes no requirements
NOTE Possible undefined behavior ranges from ignoring the situation completely with unpredictable results, to behaving during translation or program execution in a documented manner characteristic of the environment (with or without the issuance of a diagnostic message), to terminating a translation or execution (with the issuance of a diagnostic message).
Given the declaration:
int a = 5, b = 10, c;
it's likely that evaluating &b - &a
will yield a result that seems reasonable, such as 1
or -1
. (Reasonable results are always a possible symptom of undefined behavior; it's undefined, not required to crash.) But the compiler is under no obligation to place a
and b
at any particular locations in memory relative to each other, and even if it does so, the subtraction is not guaranteed to be meaningful. An optimizing compiler is free to transform your program in ways that assume that its behavior is well defined, resulting in code that can behave in arbitrarily bad ways if that assumption is violated.
By writing &b - &a
, you are in effect promising the compiler that that's a meaningful operation. As Henry Spencer famously said, "If you lie to the compiler, it will get is revenge."
Note that it's not just the result of the subtraction that's undefined, it's the behavior of the program that evaluates it.
Oh, did I mention that the behavior is undefined?
Upvotes: 9
Reputation: 612844
This code exhibits undefined behaviour because pointer arithmetic is only defined when the operands are both in the same array or struct.
The value of 1 comes about because the compiler has placed a
and b
next to each other in memory. Another compiler could do something different. Indeed the same compiler could do something different the next time you change the code.
Upvotes: 4
Reputation: 2857
C compiler knows the size of each type. for example suppose P
is a int pointer that refer to address 0x0010
. if you add P by 1 (P++ or P=P+1), then the value of P is 0x0014
.
About your question, a
and b
variables declared tandems, in physical memory
they tandems, and the head of each of them, has 4 bytes difference
with other. At this situation compiler knows the int size is 4 bytes. when you subtract 2 int pointer, the compiler divide the result by 4
.
Upvotes: 0
Reputation: 500237
In fact, the behaviour of your program is undefined. The output happens to be 1
on your platform/compiler, but it could just as easily be something else.
Upvotes: 5