adding two hex numbers

R

Ram

Hi,

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
 
A

Alexander Bartolich

Ram said:
[...]
i have two hex values

unsigned int *x = 0x37a00000
^
Here you declare a pointer to unsigned int and initialize it with
the address 0x37a00000.
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.

--
 
S

Sjouke Burry

Ram said:
Hi,

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
You are doing pointer calculations, and an int pointer
plus an int might really calculate (ptr + sizof(int)*n)
or it might crash your computer.

Also to print a pointer you might use %p instead of 0x%x.
 
S

Seebs

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
 
K

Keith Thompson

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.
 
K

Keith Thompson

Sjouke Burry said:
You are doing pointer calculations, and an int pointer
plus an int might really calculate (ptr + sizof(int)*n)
or it might crash your computer.

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.
 
P

Phil Carmody

Keith Thompson said:
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.

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
 
K

Keith Thompson

Phil Carmody said:
ITYM: unsigned int *x = (unsigned int*)0x37a00000;

Yes, you're right.

Sheesh, we're nitpicking about punctuation now? :cool:} :cool:}
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.)
 
P

Phil Carmody

Keith Thompson said:
Yes, you're right.

Sheesh, we're nitpicking about punctuation now? :cool:} :cool:}

Nope, I thought we were trying to help newbs. Sorry if it was
taken the wrong way.

Phil
 
K

Keith Thompson

Phil Carmody said:
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.
 
P

Phil Carmody

Keith Thompson said:
Phil Carmody said:
Keith Thompson said:
[...]
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;

Yes, you're right.

Sheesh, we're nitpicking about punctuation now? :cool:} :cool:}

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.

Phil
 
D

Dennis \(Icarus\)

Keith Thompson said:
Phil Carmody said:
Keith Thompson said:
[...]
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;

Yes, you're right.

Sheesh, we're nitpicking about punctuation now? :cool:} :cool:}

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.

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,161
Latest member
GertrudeMa
Top