pointers question

Discussion in 'C Programming' started by axr0284, Oct 22, 2007.

  1. axr0284

    axr0284 Guest

    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
    axr0284, Oct 22, 2007
    #1
    1. Advertising

  2. axr0284

    Richard Bos Guest

    axr0284 <> wrote:

    > 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
    Richard Bos, Oct 22, 2007
    #2
    1. Advertising

  3. axr0284

    axr0284 Guest

    On Oct 22, 12:06 pm, (Richard Bos) wrote:
    > axr0284 <> wrote:
    > > 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


    Thanks for the answer.
    Amish
    axr0284, Oct 22, 2007
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Phil
    Replies:
    1
    Views:
    639
    llewelly
    Sep 16, 2003
  2. muser
    Replies:
    3
    Views:
    753
    Ron Natalie
    Sep 18, 2003
  3. A
    Replies:
    3
    Views:
    438
    Alan Kelon
    Oct 29, 2003
  4. Chad

    pointers to pointers question

    Chad, Mar 26, 2006, in forum: C Programming
    Replies:
    5
    Views:
    303
    Default User
    Mar 27, 2006
  5. cerr

    pointers, pointers, pointers...

    cerr, Apr 7, 2011, in forum: C Programming
    Replies:
    12
    Views:
    657
Loading...

Share This Page