#include<stdio.h>
main()
{
int *p,*q,a;
p=(int *)1000;
q=(int *)2000;
a=q-p;
printf("a=%d",a);
}
Code:Code:#include<stdio.h>
#include<stdio.h>
main()
{
int *p,*q,a;
p=(int *)1000;
q=(int *)2000;
a=q-p;
printf("a=%d",a);
}
Code:Code:#include<stdio.h>
what is
=(int *)
?
1. Ask a question
2. put the code tags around ALL of your code next time.
Assuming it ever compiles, I bet you're wondering why the value of a is either 250 or 500 right, and not the 1000 you expected.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
I'm taking a guess at what you are trying to do. The following works.
Code:#include<stdio.h> main() { int x,y; int *p,*q,a; x=1000; y=2000; p=&x; /*set pointer p to hold the address of x*/ q=&y;/*set pointer q to hold the address of y*/ a=*q-*p; /*what is pointed to by q minus what is pointed to by p*/ printf("a=%d",a); }
Hi ,
You cannot just assign numbers into pointers.. the numbers need some memory and the pointers need to be pointed to this memory allocation.. this seems to be your first post.. make your problem clearer in the future...
And you name seems Indian... are you from India...
bye
vasanth
Yes you can. The value pointers store is simply a number. This number represents some portion of memory. It is a perfectly valid assignment, assuming you actually are pointing to some memory space (number) which you have access to use.Originally posted by vasanth
Hi ,
You cannot just assign numbers into pointers.. the numbers need some memory and the pointers need to be pointed to this memory allocation.
The only reason that is not a "working" assignment in the first portion of code, is because those are just some arbitrary numbers they threw in there, which they don't have rights to play around with.
Thus, if they then tried to modify what they were pointing at, the program would likely crash.
Video memory was commonly addressed this way. A fixed value was passed to a pointer, that value was where the video memory started at. It was then tweaked directly.
So in short, yes, it is legal. No, it probably won't work the way you think it should unless you have some way to know for sure what you're now pointing at. Most new OSs don't like you to directly access hardware, so direct value assignment like that is usually a BadThing(TM).
Quzah.
Hope is the first step on the road to disappointment.
Hey Sapna!
U've declared the pointers right,but u've not assigned them(or whatever u've attempted to do with them) with the correct syntax.
To learn in detail how to work with pointers,read the Schaum Series book on C.But if u are in a hurry,get E Balagurusamy's book from your library.Its an Indian publication,but from your name,I belive u r an Indian too.
If U avoid stupid abbreviations when U type then U will appear smarter to readers.U've declared the pointers right,but u've not assigned them(or whatever u've attempted to do with them) with the correct syntax.
To learn in detail how to work with pointers,read the Schaum Series book on C.But if u are in a hurry,get E Balagurusamy's book from your library.Its an Indian publication,but from your name,I belive u r an Indian too.
>read the Schaum Series book on C
For the price, that book is good, but we also have to take into account that the price is dirt cheap.
My best code is written with the delete key.
tHAT WA QUITE A SHARP UNWATED REMARK...Originally posted by Prelude
If U avoid stupid abbreviations when U type then U will appear smarter to readers.
>read the Schaum Series book on C
For the price, that book is good, but we also have to take into account that the price is dirt cheap.