mdn3din
asked on
pointer arithmetic.
Hi,
main( )
{
int *p,*q;
p=1000;
q=2000;
printf("%d",q-p);
}
can anyone explain the output? I get 500.
Thanks.
main( )
{
int *p,*q;
p=1000;
q=2000;
printf("%d",q-p);
}
can anyone explain the output? I get 500.
Thanks.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
I don't think your program can run because you set a pointer (p, q) equal to a number. In fact, compiler will understand that the number (1000, 2000 ...) is an address in memory so the pointer p, q will point to an address in memory. Usually, this address is protected by OS, so compiler will generate error:
"Memory access violation"
"Memory access violation"
integer defenition 2 bytes | 4 bytes
-------------------------- ---------- --------
Location of P 1000 | 1000
Location of P+1 1002 | 1004
.....
...
Location of Q=P+500 2000 |Q=P+250 2000
-------------------------- ---------- ---------- ---------
You will get 2 different answers depending on integer defenition
--------------------------
Location of P 1000 | 1000
Location of P+1 1002 | 1004
.....
...
Location of Q=P+500 2000 |Q=P+250 2000
--------------------------
You will get 2 different answers depending on integer defenition
melodiesoflife,
There is no compiler error like "Memory access violation"! Compiler never checks if an address is a valid one or not. Only during execution, this can be checked.
Even during execution, just by assigning an invalid address, you will not get any error or CRASH! You can always allocate to address 0 (which is invalid).......and any other invalid address....!
But, problem arises, when you try to dereference this pointer pointing to this invalid address......and your program will crash ("Segmentation fault, core dumped" or "Bus error" will be the error message) and a core containing the image of the process when the crash happened (using which you can get a stack trace and try to debug and see why it crashed....!) will get created....
....Hope I have made the point clear...!?
There is no compiler error like "Memory access violation"! Compiler never checks if an address is a valid one or not. Only during execution, this can be checked.
Even during execution, just by assigning an invalid address, you will not get any error or CRASH! You can always allocate to address 0 (which is invalid).......and any other invalid address....!
But, problem arises, when you try to dereference this pointer pointing to this invalid address......and your program will crash ("Segmentation fault, core dumped" or "Bus error" will be the error message) and a core containing the image of the process when the crash happened (using which you can get a stack trace and try to debug and see why it crashed....!) will get created....
....Hope I have made the point clear...!?
1000 . . . . . . . . . . .2000
^ ^
| |
p q
in your compilerinteger size is 2
in normal arithmetic you should get 1000.(suppose int p=1000,q=2000).
But in pointer arithmetic you will get answer depending on what it points(here it points int).p-q means that how many integers cud be stored between p and q.Ofviously if int size 2 then you can store 500 integers
if int size 4 u cud store 250 integers.
^ ^
| |
p q
in your compilerinteger size is 2
in normal arithmetic you should get 1000.(suppose int p=1000,q=2000).
But in pointer arithmetic you will get answer depending on what it points(here it points int).p-q means that how many integers cud be stored between p and q.Ofviously if int size 2 then you can store 500 integers
if int size 4 u cud store 250 integers.
ASKER
Hi,
Tried this,
void main(void)
{
int i,*q;
q=2000;
for(i=1;i<=1000;i++)
printf("%d-%d\t",i,q-i);
}
I get 1-1998 2-1996 ... 1000-0.So my integers are two bytes.
When I change 'int i' to 'int *i' I get, 1-999 3-998 5-997 ... 999-500.
When I change 'q-i' to 'q-(int)i' I get, 1-1998 3-1994 ... 999-2.
(pointer +/- pointer) : Normal arithmetic on pointers.
(pointer +/- integer) : Pointer arithmetic on pointers.
???
Tried this,
void main(void)
{
int i,*q;
q=2000;
for(i=1;i<=1000;i++)
printf("%d-%d\t",i,q-i);
}
I get 1-1998 2-1996 ... 1000-0.So my integers are two bytes.
When I change 'int i' to 'int *i' I get, 1-999 3-998 5-997 ... 999-500.
When I change 'q-i' to 'q-(int)i' I get, 1-1998 3-1994 ... 999-2.
(pointer +/- pointer) : Normal arithmetic on pointers.
(pointer +/- integer) : Pointer arithmetic on pointers.
???
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
However, if it is pointing to an array of integers, and if integers are 4 bytes on your machine, then adding 1 will add *4* to the address, so that it will now point to the next integer.
Pointer subtraction is similar. I would assume that you in fact have a compiler/system in which integers are 2 bytes. Thus the number of integers between 1000 and 2000 would be 500.