Store pointee to an int variable

This is a discussion on Store pointee to an int variable within the C Programming forums, part of the General Programming Boards category; Hi all, I am trying to assign a pointee into an integer and subtract a constant from that. Is this ...

  1. #1
    Registered User
    Join Date
    Jun 2009
    Posts
    26

    Store pointee to an int variable

    Hi all,

    I am trying to assign a pointee into an integer and subtract a constant from that. Is this possible?

    I have the following code:

    Code:
    #define OFFSET 0xC0000000
    uint32_t *Pointer_A = (uint32_t*)0xDF3F1E00;
    uint32_t Variable_A = Pointer_A - OFFSET;
    printk("Variable_A is: 0x%X \n", Variable_A);
    This gives 0xDF3F1E00 instead of the desirable 0x1F3F1E00.

    Any hints?

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You aren't dereferencing Pointer_A. You are just subtracting from its address, not its value. See also: ptrdiff


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Jun 2009
    Posts
    26
    Quote Originally Posted by quzah View Post
    You aren't dereferencing Pointer_A. You are just subtracting from its address, not its value. See also: ptrdiff
    Thanks for your reply. I don't want to dereference Pointer_A, I just want to subtract an OFFSET from the address Pointer_A points to and store the result into a variable (Variable_A).

    Thanks.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You need to be doing the math as unsigned instead of signed.
    Code:
    uint32_t Variable_A = (uint32_t)Pointer_A - 0xC0000000;
    (EDIT: As cas says below, I mean int rather than pointer.)
    Last edited by tabstop; 05-25-2011 at 02:49 PM.

  5. #5
    cas
    cas is offline
    Registered User
    Join Date
    Sep 2007
    Posts
    991
    You generally want to avoid converting between pointers and integers. However, you appear to be doing something non-portable anyway, so I'll assume these conversions make sense.

    You appear to want to subtract a particular number of bytes from a pointer. The problem is that when you add to/subtract from a pointer, the compiler scales the value in terms of the size of the pointed-to object. So a uint32_t* points to a 4-byte object (pedants: it's probably OK to assume 8-bit bytes here), which means when you add 1 to a uint32_t*, you're actually adding 4 bytes (this way each time you increment a uint32_t*, it points to the next uint32_t).

    The easiest way, of course, would be to subtract 0xC0000000 from 0xDF3F1E00, as an integer. So, given your code, you can try:
    Code:
    uint32_t Variable_A = (uint32_t)Pointer_A;
    Variable_A -= OFFSET;
    You can also use the fact that an unsigned char is one byte to get the pointer arithmetic to scale by bytes:
    Code:
    uint32_t Variable_A = (uint32_t)((unsigned char *)Pointer_A - OFFSET);
    There's no guarantee that the result will be aligned properly, but I presume that's something you're aware of.

  6. #6
    Registered User
    Join Date
    Jun 2009
    Posts
    26
    Thank you all for your replies (specially cas for the detail explanation given!). What tabstop suggested in the first place worked like a charm. Thanks again.

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Sorry, what you have should be fine, provided you actually mean printf and not printk. Also, you need to pay attention to your compiler, which is telling you:

    "warning: initialization makes integer from pointer without a cast"

    Edit - beaten twice.


    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    printk is printk() if I'm not wrong.
    Not sure what he's doing according to his posts.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how i can store 2^99999 in integer variable
    By 62049913377 in forum C Programming
    Replies: 7
    Last Post: 08-07-2010, 01:09 AM
  2. which variable can store words?
    By Hunterofman in forum C++ Programming
    Replies: 8
    Last Post: 04-28-2008, 05:59 PM
  3. How do i store today's date in variable?
    By Brain Cell in forum C Programming
    Replies: 12
    Last Post: 08-23-2004, 06:24 AM
  4. store date in a variable
    By actionbasti in forum C++ Programming
    Replies: 2
    Last Post: 10-01-2003, 09:50 PM
  5. Store command output into a variable.
    By Kevin.j in forum C Programming
    Replies: 7
    Last Post: 09-29-2002, 11:44 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21