Problems with creating a pointer to struct at a specific address

This is a discussion on Problems with creating a pointer to struct at a specific address within the C Programming forums, part of the General Programming Boards category; Using a bit of conditional compilation to isolate the machine dependencies should enable you to get a lot of testing ...

  1. #16
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,821
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

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

Page 2 of 2 FirstFirst 12
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, 06:25 AM
  4. Pointer to specific memory address
    By elnerdo in forum C++ Programming
    Replies: 10
    Last Post: 05-19-2006, 08: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


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