Thread: Structure Question

  1. #1
    Registered User
    Join Date
    Dec 2008
    Posts
    12

    Structure Question

    I wanted to ask, if I had a struct which looked like this:

    Code:
    typedef struct types
    {
        char *data1;
        int *data2;
        int data3
    }type;
    
    int main(....)
    {
        type a;
        type *temp = NULL;
        temp = &a + 1 *sizeof(char*) + 1*sizeof(int*);
     ......
    }
    Is the example correct? how can i get to int data3 without using the . or -> operator?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You need a semicolon after data3. If, for some reason, you need to get there without a . or a ->, I suppose you could use offsetof and some fancy pointer arithmetic.

  3. #3
    Registered User
    Join Date
    Dec 2008
    Posts
    12
    yea but what would the pointer arithetic be? thats actually my question

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    offset gives you the offset from the start of the structure. You'll have to cast a pointer to the object to a char* (so that you can do arithmetic in bytes), and then add the offset to it to get a pointer to the specific data element you want, and then you have to dereference it as an int* to get the correct value.

  5. #5
    Registered User
    Join Date
    Dec 2008
    Posts
    12
    Thank you very much for the info, ill try it and see.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > temp = &a + 1 *sizeof(char*) + 1*sizeof(int*);
    Erm, No actually.
    For one thing, structures can have padding space, so the offset of any member (except the first) is not necessarily the sum of the sizes of the preceding members.

    Second, this is just far easier.
    temp = &a.data3;
    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.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    No to mention guaranteed to work on all compilers and platforms
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 13
    Last Post: 12-14-2007, 03:34 PM
  2. Simple C# structure question
    By sketch in forum C# Programming
    Replies: 4
    Last Post: 09-14-2007, 04:29 PM
  3. data structure question
    By miami_victor in forum C++ Programming
    Replies: 13
    Last Post: 12-31-2004, 12:56 AM
  4. Array and Structure Question
    By loopshot in forum C++ Programming
    Replies: 2
    Last Post: 10-21-2004, 05:10 PM
  5. structure question
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 12-03-2001, 09:04 PM