Ram said:
sorry for asking silly Question
i have two hex values
unsigned int *x = 0x37a00000
Others have said that you're initializing a pointer to the address
0x37a00000. In fact, what you're trying to do is initialize
a pointer object with an integer value. Since there are no
implicit conversions from integer types to pointer types (other
than the special case of a null pointer constant, which isn't what
you're using here), the declaration above is actually a constraint
violation. You should, at the very least, have gotten a warning
from your compiler -- more likely a syntax error message if the
semicolon really is missing, as it is in what you posted.
For historical reasons, many compilers will (after printing a warning)
generate an implicit conversion, making the above equivalent to the
legal declaration
unsigned int *x = (unsigned int)0x37a00000;
But strictly speaking the language doesn't imply that this is whath
should happen. The declaration, as you've written it, is ill-formed
and has no meaning (unless your particular compiler chooses to give it
one).
If you did get a warning, you should have mentioned it; that's a more
important piece of information than the program's run-time output.
And if you didn't get a warning, either you're not using your compiler
properly or the code you compiled is substantially different from what
you posted.
unsigned int *y;
*y = *x + 0x300000;
This would be ok *if* x and y pointed to valid memory locations.
printf("*final val = 0x%x\n",*y);
it should print o/p as 0x37d00000
but its printing output as 0x300000
am i doing blunder??
Yes, but it's impossible to tell exactly what your blunder is.
My best guess is that both x and y happen, by sheer chance to point
to memory locations that behave as if they're valid, and that the
location x points to happens to contain the value 0.
We could have saved some time if you had posted the *exact*
source code that you fed to the compiler (copy-and-paste, *don't*
re-type it). That means a complete self-contained program that
we can copy-and-paste and try ourselves. By summarizing the code
rather than pasting it, you introduce additional errors, and we
can't possibly be sure which errors are in your original source
and which you introduced later.
The real problem, I suspect, is that (a) you're mis-using pointers,
using them without ensuring that they point to valid memory, and (b)
you're using pointers at all where there's no need for them.