Addresses and extracting the value stored at an address

This is a discussion on Addresses and extracting the value stored at an address within the C Programming forums, part of the General Programming Boards category; Hi, - I am in doubt whether or not the following C code will do as it is intended. For ...

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    3

    Addresses and extracting the value stored at an address

    Hi,
    - I am in doubt whether or not the following C code will do as it is intended. For example, does it - as expected - take a whole array (called vector) as input, or is there something very basic in the syntax which makes that not possible as it is now?

    - As i've worked in matlab for years, i have forgotten a lot of the fine details in C - like addresses and pointers. I expect the LUT_BASE define is alright - but will *(LUT_BASE+k) take the value at address (LUT_BASE+k) ?
    For example, if k is 2, and the memory at address location 0x001002 contains the integer 43 - would *(LUT_BASE+2) then return 43?

    - I'm also a little bit confused about the addresses to increment to get the next number in the look-up table. Each number in the look-up-table is stored as signed integers (16 bit) - but wouldn't that mean that i have to increase the addresses for each iteration by 5 (2^4=16, and increment 1 to the next address to find the start)?

    Thanks for any hints.

    Code:
    #include <stdio.h>
    #define BLOCK_SIZE 2048
    #define LUT_BASE ( char *) 0x001000f
    
    void main(int **permute, int *array)
    {
     int k;
     int permute[BLOCK_SIZE];
        for(k=0; k<=BLOCK_SIZE-1; k++)
            {
            permute[k]=array[*(LUT_BASE+k)];
            }
     return;
    }
    Last edited by donoe_85; 04-08-2009 at 01:20 AM.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You need to define a poiner (e.g. char * or int *, depending on what is actually at LUT_BASE), and then assign that pointer to LUT_BASE (using a cast to make the compiler happy that you actually DO want to assign an integer to a pointer). [It is technically possible to do this using no pointer, but it's messy and generally no better].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Apr 2009
    Posts
    3
    Thanks,
    Hmm something along these lines?
    The pointer of p should then be pointing at LUT_BASE

    Code:
    #include <stdio.h>
    #define BLOCK_SIZE 2048
    #define LUT_BASE ( char *) 0x001000f
    
    void main(int **permute, int *array)
    {
     int *p;
     p* = (int)LUT_BASE;
     int k;
     int permute[BLOCK_SIZE];
        for(k=0; k<=BLOCK_SIZE-1; k++)
            {
            permute[k]=array[p+k];
            }
     return;
    }

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    wrong cast - it should be
    Code:
    #define LUT_BASE (int *)0x1000f;
    int *p = LUT_BASE;
    and you need to dereference the pointer:
    Code:
    permute[k]=array[*p+k];

    Also, is LUT_BASE a byte or 32-bit value? If it's a 32-bit value, then you should have a int *, if it's a byte you should have unsigned char (or char, if you want negative values).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Apr 2009
    Posts
    3
    Now after some time - that actually makes sense. I don't recall why i had #define LUT_BASE ( char *) 0x001000f with a char to begin with actually, it should have been int, 32 bit.
    Thanks again

Popular pages Recent additions subscribe to a feed

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