Reputation: 1621
#include <stdio.h>
#include <conio.h>
void main()
{
int m = 20;
int n = 30;
int *x = (int *)m;
int *y = (int *)n;
printf("%d", y-x); //Output is 5
}
How is the output 5? One of the reason could be that x and y consider 20 and 30 as address and during pointer arithmetic the value could be (30-20)/(size of int) which is 10/2 = 5.
My doubt is what is the difference between returning pointer and returning address ? Why the address of m was not stored in pointer variable x?
Upvotes: 0
Views: 1558
Reputation: 223699
The address of m
wasn't stored in x
because you didn't assign it the address of m
. You assigned it the value of m
. The cast you applied masked the fact that you attempted to assign an integer to a pointer, which the compiler would have warned you about.
If you want to use the address of a variable, use the address-of operator &
:
int *x=&m;
int *y=&n;
You are correct regarding why the output is 5. The values of m
and n
are assigned to pointers and are thus treated as addresses.
Note however that pointer subtraction is undefined unless both operands point to members of the same array (or one past the end of that array). Also note that it is undefined behavior to print pointers with the %d
format specifier. You need to use %p
instead, and you need to cast the given parameter to void *
(one of the rare cases where a cast to/from void *
is required).
Upvotes: 3
Reputation: 1
int *x=(int *)m;
int *y=(int *)n;
By doing this what you would be doing is -
in a pointer x
- you put the value m
( Treat this as a pointer as well as you are doing int *
.
Hence x
would have a value of 20 ( 0x14 ) and y
would have a value of 30 ( 0x1E).
Pointer arithmetic is bit different from normal arithmetic operation https://www.tutorialspoint.com/cprogramming/c_pointer_arithmetic.htm
And about assigning address you should use a &variable to do that
Upvotes: -2
Reputation: 166
You have to use %p if you would like to show the address that is stored in a pointer. In your code, you should use * before the name of the pointer.
#include<stdio.h>
int main()
{
int m=20; int n=30;
int *x= &m;
int *y= &n;
printf("\n%d", (*y)-(*x)); //Output is 10
printf("\n%p address of m", x);
printf("\n%p address of n", y);
return 0;
}
Upvotes: -1
Reputation: 234645
The behaviour of your program is undefined.
Pointer arithmetic - including the difference between two pointers - is only valid within arrays (including one past the end of the arrray), or between the address of a scalar and one past that.
The behaviour of your casts from an int to a pointer to an int is also undefined.
If you do have a valid difference between two pointers, then use the format specifier %td to output it.
Upvotes: 4
Reputation: 67
if you want to store the adress of m in x simply do int* x = &m; the casting to int type pointer here is causing the problem in my optinion. also if you want to calculate the values of y-x then do *y-*x. as it is in your code you calculate the adress of n - the adress of m.
Upvotes: 0