Thread: Problems with creating a pointer to struct at a specific address

  1. #16
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Using a bit of conditional compilation to isolate the machine dependencies should enable you to get a lot of testing done on the host.
    Eg
    Code:
    #include <stdio.h>
    
    typedef struct FOO_tag{
        int     PER;
        int     PDR;
        int     PSR;
    } FOO;  /* don't typedef pointer types *pointerFOO; */
    
    #if defined TARGET
    #define INIT_ADR ((FOO*)0x12345678)
    FOO *getMyFoo ( void ) {
        FOO *result = INIT_ADR;
        return result;
    }
    #elif defined HOST
    FOO *getMyFoo ( void ) {
        static FOO test = {
            1, 2, 3 /* useful data */
        };
        return &test;
    }
    #else
    #error neither HOST or TARGET specified
    #endif
    
    int main ( ) {
        FOO *p = getMyFoo();
        printf("PER=%d\n", p->PER );
        return 0;
    }

    $ gcc -DHOST foo.c
    $ ./a.out
    PER=1
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  2. #17
    Registered User
    Join Date
    Jul 2012
    Posts
    6
    That is actually a very nice and useful solution, thank you Salem

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 2 problems: Struct array and pointer + struct
    By v1n1c1u5 in forum C Programming
    Replies: 0
    Last Post: 12-13-2009, 05:38 PM
  2. Struct Pointer Problems. (Warning: Long Post.)
    By Phoenix940 in forum C Programming
    Replies: 1
    Last Post: 11-30-2008, 10:04 PM
  3. Allocate a variable at a specific address
    By mermaidsrule in forum C Programming
    Replies: 11
    Last Post: 05-03-2007, 05:25 AM
  4. Pointer to specific memory address
    By elnerdo in forum C++ Programming
    Replies: 10
    Last Post: 05-19-2006, 07:35 AM
  5. Assigning memory address of member struct to pointer.
    By Tronic in forum C++ Programming
    Replies: 2
    Last Post: 03-20-2004, 05:53 PM

Tags for this Thread