Finding Struct Types

This is a discussion on Finding Struct Types within the C Programming forums, part of the General Programming Boards category; In an effort to keep some level of OO in this C version of a C++ program without going overboard ...

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    7

    Finding Struct Types

    In an effort to keep some level of OO in this C version of a C++ program without going overboard with vtable macros and such, I have created 3 kinds of structs which each store information relating to the 3 types of Objects that I used to have in C. Now, to store them in the same array so that they may maintain order and readability, I want to stick them into an array of void pointers. Now the my question is this. Am I able to reference the attributes of the structs pointed to by the void pointers without first casting them back to their original type? If not, is there any other way that I could find out what type of struct the void pointer actually points to? Any help would be appreciated.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    No. You have to keep track of what type it is. You must typecast any use of a void ponter, unless you're assigning a void pointer to something, in which case, you shouldn't.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    7
    Giving it a bit more thought, it occured to me the easiest way to find out what type of struct it is to store a variable/s which tell me what kind of struct it is. Of course, being unable to do this with void pointers, I had the idea that I could typecast the void pointer to each of my know struct types in turn, and then compare the value of the identifier that way. For example, I typecast the void pointer to x StructTypeOne, and then use x.structType as a condition for a selection statement. If structType is not the type I want, I then typecast it to the next type, and so on until the types match. While I am fairly certain this would work, it doesn't appear very efficient or elegant. Is this the only way I can get around the problem or is the a more efficient solution. Thanks for the information :P

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    struct datatype
    {
        size_t type;
        void * data;
    };
    Use an array of this type of structure.
    Code:
    size_t type[ X ];
    Use an array of type as a catalog of sorts, which stores in its position the type of the corresponding array of your void pointers. There are a few different ways you can do it.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Converting from C to C++
    By Taka in forum C++ Programming
    Replies: 5
    Last Post: 04-08-2009, 03:16 AM
  2. Struct types and dynamic arrays
    By simone.marras in forum C Programming
    Replies: 6
    Last Post: 03-14-2009, 11:56 AM
  3. memory issue
    By t014y in forum C Programming
    Replies: 2
    Last Post: 02-21-2009, 12:37 AM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 04:00 PM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 02:27 PM

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