C: Pointers to any type?

Posted by dragme on Stack Overflow See other posts from Stack Overflow or by dragme
Published on 2010-05-29T17:22:12Z Indexed on 2010/05/29 17:32 UTC
Read the original article Hit count: 267

Filed under:

I hear that C isn't so type-safe and I think that I could use that as an advantage for my current project.

I'm designing an interpreter with the goal for the VM to be extremely fast, much faster than Ruby and Python, for example.

Now I know that premature optimization "is the root of all evil" but this is rather a conceptual problem.

  • I have to use some sort of struct to represent all values in my language (from number over string to list and map)

Would the following be possible?

struct Value {
 ValueType type;
 void* value;
}
  • I would store the actual values elsewhere, e.g: a separate array for strings and integers, value* would then point to some member in this table.

  • I would always know the type of the value via the type variable, so there wouldn't be any problems with type errors.

Now:

Is this even possible in terms of syntax and typing?

© Stack Overflow or respective owner

Related posts about c