newbie: pointers to structure questions

This is a discussion on newbie: pointers to structure questions within the C Programming forums, part of the General Programming Boards category; Hi its me again, im now looking into structures at im trying to figure out the meaning of the following ...

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    61

    Question newbie: pointers to structure questions

    Hi its me again,

    im now looking into structures at im trying to figure out the meaning of the following code.

    lets say we have 2 structures:
    Code:
    struct struct1{
          double i, a;
    };
    
    
    struct struct2 {
         struct struct1 results[2];
    };
    
    struct struct1 *s1;
    struct struct2 s2;
    
    s1 = (struct struct1*)&s2  // what does this means????????
    i dun get what the commented line means. could someone please explain it to me? i appreciate all the help given to me

  2. #2
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    What you're looking at is a "gross hack" which is unfortunately possible in C. Understanding this, will not help you to understand structures any better. What you will get is a massive headache.

    Move on to a better example, I think.

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    61
    i still wish to know whats happening though heh

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,712
    I am not sure what it means, but what it does is make s1 point to a struct2 object. Effectively, you are reinterpreting the struct2 object as one or more struct1 objects. In this case the effect is probably the same as s1 = s2.results, which is much easier to understand and is actually meaningful.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Apr 2008
    Posts
    61
    hey laserlet, thanks for make this clear for me. at least now i got an idea of what this does

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,587
    http://cpwiki.sf.net/A_pointer_on_pointers
    A pointer is only a variable with an address. So the & operator simple takes the address of the first struct and then an explicit cast is used to tell the compiler to make it into a pointer to struct2 and assigns it to the variable.
    The type tells the compiler what value it is to expect, but in the low level stuff, there's no such thing as type, so you can cast it to whatever and the compiler will be happy, but your program will break.

    It's quite a useful hack, if you use it properly.
    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.

  7. #7
    Banned
    Join Date
    Nov 2007
    Posts
    678
    how is this hack useful??

  8. #8
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    A dynamically allocated 2-dimensional+ array for example.

  9. #9
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,587
    (NOTE: C++)
    Polymorphism might be another:
    Code:
    pp< std::vector< pp<CItemData> > > CListBox::GetAllItemData()
    GetAllItemData returns a vector of CItemData objects. However...
    Code:
    typedef pp< std::vector< pp< Stuff::CItemDataTemplate<CString> > > > ItemDataVector;
    ItemDataVector pData = *(ItemDataVector*)&m_Logs.GetAllItemData();
    I stored Stuff::CItemDataTemplate<CString> objects which I need to retrieve. Since it's derived from CItemData, it's perfectly fine to add it, but then I need to cast it back or use virtual functions to manipulate the data.
    So in this case, a hack might be justified.
    Last edited by Elysia; 04-09-2008 at 04:16 AM.
    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.

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,712
    Note that this thread is in the C programming forum.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,587
    There may be many uses for such a hack, even in C++, as well as C. I'll just put a note, since manav did ask and is using C++, as well.
    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.

  12. #12
    Registered User
    Join Date
    Apr 2008
    Posts
    61
    thanks Elysia for more in depth explaination.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A very long list of questions... maybe to long...
    By Ravens'sWrath in forum C Programming
    Replies: 16
    Last Post: 05-16-2007, 05:36 AM
  2. Newbie Questions
    By snoopy1 in forum C++ Programming
    Replies: 3
    Last Post: 11-19-2006, 01:00 PM
  3. Im a newbie to C and i have some questions
    By pave1104 in forum C Programming
    Replies: 5
    Last Post: 07-05-2006, 09:48 PM
  4. pointers, arrays, and functions oh my (newbie Q)
    By eazhar in forum C++ Programming
    Replies: 6
    Last Post: 07-21-2002, 06:26 PM
  5. Real newbie with VC6 questions
    By MagiZedd in forum Windows Programming
    Replies: 8
    Last Post: 10-15-2001, 08:27 PM

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