pointers question

A

axr0284

Hi,
I would like to know what this code line does in this function
Code:
void FlashDrvWriteByte(int iAddr, int iByte)
{
        *(int*)iAddr = (iByte & 0xFF);

}

I am not sure if this group uses code tags so bear with me here.
I would like a description of what " *(int*)iAddr " means. I think
it's casting the int to a "pointer to an int" and then it's assigning
the memory location the pointer points to with the value of (iByte &
0xFF). Am I correct here? Thanks
Amish
 
R

Richard Bos

axr0284 said:
I would like to know what this code line does in this function
void FlashDrvWriteByte(int iAddr, int iByte)
{
*(int*)iAddr = (iByte & 0xFF);
}
I would like a description of what " *(int*)iAddr " means. I think
it's casting the int to a "pointer to an int" and then it's assigning
the memory location the pointer points to with the value of (iByte &
0xFF). Am I correct here? Thanks

Yes. As there is nothing to guarantee that iAddr contains a value which,
when converted to int *, is a valid, well-aligned pointer to int, this
is allowed to crash randomly as far as ISO C is concerned.
However, in some system-specific circumstances which you do want to do
this and the rest of your system guarantees that that int _does_ convert
to a usable int *. In those cases, the above is the correct ISO C way to
express "I have an address written as a number rather than a proper
pointer; please assume that I know what I'm doing, and write through
it."

IOW, don't do this unless you're very sure that you're not going to try
and write over a completely random memory address; but when you _are_
sure of that, do it like that.

Richard
 
A

axr0284

Yes. As there is nothing to guarantee that iAddr contains a value which,
when converted to int *, is a valid, well-aligned pointer to int, this
is allowed to crash randomly as far as ISO C is concerned.
However, in some system-specific circumstances which you do want to do
this and the rest of your system guarantees that that int _does_ convert
to a usable int *. In those cases, the above is the correct ISO C way to
express "I have an address written as a number rather than a proper
pointer; please assume that I know what I'm doing, and write through
it."

IOW, don't do this unless you're very sure that you're not going to try
and write over a completely random memory address; but when you _are_
sure of that, do it like that.

Richard

Thanks for the answer.
Amish
 

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

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top