sorry for asking silly Question
i have two hex values
unsigned int *x = 0x37a00000
unsigned int *y;
*y = *x + 0x300000;
printf("*final val = 0x%x\n",*y);
it should print o/p as 0x37d00000
but its printing output as 0x300000
am i doing blunder??
Thanks in advance
-Ram
> unsigned int *y;
Another pointer. It is not initialized.
> *y = *x + 0x300000;
On the left side of the assignment you dereference an unitialized
pointer. This is likely to crash.
On the right side you read the unsigned int at address 0x37a00000.
This will yield some random value.
> printf("*final val = 0x%x\n",*y);
>
> it should print o/p as 0x37d00000
>
> but its printing output as 0x300000
Well, you are lucky. Writing to the random address did not crash.
And the unsigned int at address 0x37a00000 has the value 0.
> am i doing blunder??
Indeed.
--
Also to print a pointer you might use %p instead of 0x%x.
> Hi,
>
> sorry for asking silly Question
>
> i have two hex values
>
> unsigned int *x = 0x37a00000
You are declaring a *pointer* named x, and setting it to an
arbitrary value.
> unsigned int *y;
Here you have a pointer that doesn't point anywhere.
> *y = *x + 0x300000;
Here, you take the contents of your arbitrary-value pointer, and
add something to it, and store the result in something you've never
heard of.
> printf("*final val = 0x%x\n",*y);
>
> it should print o/p as 0x37d00000
Please don't use abbreviations like "o/p". They are easy for people to
misunderstand and don't save much time to begin with.
> but its printing output as 0x300000
>
> am i doing blunder??
Yes.
Try:
unsigned int x = 0x37d000000;
unsigned int y;
y = x + 0x3000000;
printf("final val = 0x%x\n", y);
You don't need the '*' because you don't actually intend to use pointers here.
-s
--
Copyright 2010, all wrongs reversed. Peter Seebach / usenet...@seebs.net
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
> i have two hex values
>
> unsigned int *x = 0x37a00000
> unsigned int *y;
>
> *y = *x + 0x300000;
>
> printf("*final val = 0x%x\n",*y);
>
> it should print o/p as 0x37d00000
>
> but its printing output as 0x300000
On a side note of what was already said, you may consider using
intrptr_t if you really need to do such low-level arithmetic on
pointers and later cast it to a pointer.
--
Best regards, _ _
.o. | Liege of Serenly Enlightened Majesty of o' \,=./ `o
..o | Computer Science, Michal "mina86" Nazarewicz (o o)
ooo +--<mina86-tlen.pl>--<jid:mina86-jabber.org>--ooO--(_)--Ooo--
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.
--
Keith Thompson (The_Other_Keith) ks...@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
He's mis-using pointers, but he's not doing pointer arithmetic. x and
y are pointers, but *x and *y are not; they're of type unsigned int.
The statement
*y = *x + 0x300000;
would be valid if y and x pointed to valid unsigned int objects, and
it's doing an unsigned int addition, not a pointer addition.
> Also to print a pointer you might use %p instead of 0x%x.
The printf call is also correct (or would be if y were valid); again,
*y is of type unsigned int. He's not trying to print the pointer
value.
Reemphasis to original poster - post exactly what you compile, and
compile exactly what you post. If the compiler complains, post exactly
what it reports.
> 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;
ITYM: unsigned int *x = (unsigned int*)0x37a00000;
I hope such compilers are all on media which can no longer be read! ;-)
Phil
--
I find the easiest thing to do is to k/f myself and just troll away
-- David Melville on r.a.s.f1
Yes, you're right.
Sheesh, we're nitpicking about punctuation now? 8-)} 8-)}
> I hope such compilers are all on media which can no longer be read! ;-)
I think *most* C compilers do this.
Given:
unsigned int *x = 0x37a00000;
gcc says:
c.c:1: warning: initialization makes pointer from integer without a cast
and Sun's cc says:
"c.c", line 1: warning: improper pointer/integer combination: op "="
For any translation unit that contains a constraint violation (or
even a syntax error), once the compiler has issued a diagnostic,
even a non-fatal warning, it's done its job; the Standard doesn't
care what it does after that. (I personally would prefer it if the
Standard required such translation units to be reject in conforming
mode, but it doesn't.)
reserve in the stack the space of one pointer called x
and full that space with the value 0x37a00000
> unsigned int *y;
reserve in the stack the space of one pointer y
> *y = *x + 0x300000;
get the value in x == 0x37a00000
and doing *y=*0x37a00000+0x300000
something like
&x= address of the space reserved
x=*&x is what that space contain here is 0x37a00000
mov eax, [0x37a00000] ; the same mov eax, [x]
add eax, 0x300000
mov [y], eax
> printf("*final val = 0x%x\n",*y);
print * of what the space y contain
> it should print o/p as 0x37d00000
>
> but its printing output as 0x300000
so could this mean that 0x37a00000 is in the address space
like is in the address space the value y,
of the program and dword*0x37a00000==0?
all this modulo possibile errors i show
> am i doing blunder??
>
> Thanks in advance
> -Ram
#include <stdio.h>
int main(void)
{
// it seems to me here you want all pointer so *
char *x;
char *y;
unsigned *xu;
unsigned *yu;
// here initialize these pointers
// in the few i know people here say assign
// to pointers not valid address for the program
// e.g possibily x=2 in this case
// should be not allow
// but i not like what they say
x=0x37a00000;
y = x + 0x300000;
printf("final val for chars = 0x%x\n", y);
// this should print 0x37d00000
xu=0x37a00000;
yu = xu + 0x300000;
printf("final val for unsigned = 0x%x\n", yu);
// in this machine unsigned is 4 chars so
// sum 0x300000 is like sum 4*0x300000==0xC00000 chars
// xu + 0x300000== (char*)xu + 4*0x300000
// ==0x37a00000+0xC00000== 0x38600000
/*
it should print o/p as 0x37d00000
but its printing output as 0x300000
am i doing blunder??
*/
return 0;
}
/*
final val for chars = 0x37d00000
final val for unsigned = 0x38600000
*/
this above means
unsigned int *x; // reserve space with address &x, value *&x==x
x=0x37a00000; // assign to it value
and *not*
unsigned int *x;
*x=0x37a00000;
> unsigned int *y;
Nope, I thought we were trying to help newbs. Sorry if it was
taken the wrong way.
Um, did you miss the smileys?
It was a significant correction, and I thank you for making it.
Sorry, I was a bit on the braindamaged side yesterday.
I didn't. FWIW, I thought it especially amusing given that had commented on
original poster's missing semi-colon after the declaration of x :-)
Dennis