Thread: accessing a scruct element by relative address?

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    2

    Question accessing a scruct element by relative address?

    Hi,

    I a large struct named DATA with different datatypes and I want to iterate through it as following:
    I want to create an array of struct called TYPES, that describe where and what kind of data have.
    Then I want to go through that array and get the date from DATA and processit according to it's type.

    How can I get the relative address of a struct element, so I can write it into a TYPES struc as the "where" data?

    Thank you!

    Greeting from Germany
    Christoph

    P.S.: Please let me know if something in my descripion wasn't understandable, my english isn't that good.
    Last edited by Birdy27; 02-11-2006 at 06:09 AM.

  2. #2
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    The & operator
    Code:
    #include <stdio.h>
    
    struct DATA
    {
        int y;
        int x;
    };
    
    int main(void)
    {
        struct DATA someData;
        someData.x = 10;
        
        printf("%p\n",&someData);
        printf("%p\n",&someData.x);
        
        getchar();
        
        return 0;
    }
    Woop?

  3. #3
    Registered User
    Join Date
    Feb 2006
    Posts
    2
    Das Problem an der sache ist, dass ich stets ein neues Struct als Zeiger übergeben bekomme (iTunes trackInfo).

    Can I then use
    Code:
    Ptr xRelAdr = ((Ptr)&someData.x - (Ptr)&someData); // int is probably the wrong type
    
    (int)(&someData+xRelAdr)*=27;
    So that I can save the relative address (xRelAdr in this case) and reuse it whenever I get a new pointer to that struct?

    Is "Ptr" the right type for a pointer?
    Last edited by Birdy27; 02-11-2006 at 07:41 AM.

  4. #4
    old man
    Join Date
    Dec 2005
    Posts
    90
    The standard type for that is "ptrdiff_t" as defined in stddef.h

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM
  2. sorting
    By penny_731729 in forum C Programming
    Replies: 3
    Last Post: 04-28-2003, 10:56 AM
  3. Binary searches
    By Prezo in forum C Programming
    Replies: 4
    Last Post: 09-10-2002, 09:54 PM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM
  5. doubly linked lists
    By cworld in forum C++ Programming
    Replies: 2
    Last Post: 04-21-2002, 09:33 AM