Structure Question

This is a discussion on Structure Question within the C Programming forums, part of the General Programming Boards category; I wanted to ask, if I had a struct which looked like this: Code: typedef struct types { char *data1; ...

  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,185
    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,185
    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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,333
    > 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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,165
    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.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 13
    Last Post: 12-14-2007, 02: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-30-2004, 11:56 PM
  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, 08:04 PM

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