Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

Can I store a type as a variable in C?

Name: Anonymous 2008-06-08 7:24

printf("Hello World, /prog/\n");

I was wondering if there is a good way to store a C data type as a variable, and later use that variable for the declaration of return types and variable types (in the form of a cast would be fine), or as function argument for polymorphic fun.

Name: Anonymous 2008-06-08 7:28

typedef

Name: Anonymous 2008-06-08 7:33

>>2
No, I want to be able to hold in a variable "This is an int" or "This is a float" (for any arbitrary type), not rename int to hurrdurr_t.

Unless there is some clever trick using typedef of which I am unaware.

Name: Anonymous 2008-06-08 7:59

                       //`'''```,
             o        // LISP   `.,
       ,....OOo.   .c;.',,,.'``.,,.`
    .'      ____.,'.//
   / _____  \___/.'
  | / ||  \\---\|
  ||  ||   \\  ||
  co  co    co co

Name: Anonymous 2008-06-08 8:05

>>4
No fuck you; I'm doing this because I want to learn how to implement crazy shit at a lower level. Or do you think that all of your wonderful higher-level programming structures were shat fully-formed from the immaculate anus of Paul Graham?

Name: Anonymous 2008-06-08 8:06

>>1
__typeof__

Name: Anonymous 2008-06-08 8:13

>>6
Nice; thank you!

Name: Anonymous 2008-06-08 8:31

>>7
__typeof__ is an extension supported by some compilers.
It's not really standard C. What you're asking for cannot be done that way. You have to find another solution if you want your code to be portable.

Name: Paul Graham !LISPHymEeU 2008-06-08 11:04

>>5
do you think that all of your wonderful higher-level programming structures were shat fully-formed from the immaculate anus of Paul Graham
But! They are!

>>1,6,8
Use C++'s typeid instead, it's much nicer. Then you don't need a type argument since your function can just find it out itself.

I don't see why you really need to do that though, usually a polymorphic function should only use the common interface for all the objects it takes. It's part of the abstraction, if some objects need specific operations it should be hidden in their method and not handled by the polymorphic function. There are some valid uses for it, but just like a code full of casts, it may hint at a design problem.

later use that variable for the declaration of return types and variable types
You can't declare at runtime. You mean force the output with a cast to be the type you declared? This is nasty, if you want to learn anything useful try to make your code as simple and maintainable as possible (eg: avoid this). Like, return a pointer to a base class or something. Besides, your function can't take in random objects, unless you make it take a void* pointer - which is also nasty shit, and you won't be able to dereference it anyway.

If you want a function that takes multiple types and there's any way at all of making it known at compile-time, try to template it instead.

Name: Anonymous 2008-06-08 11:23

>>9
c is a lot better than that shitty toy language.

Name: Anonymous 2008-06-08 12:15

>>10
Wait, did you just call Sepples a ``toy language''?

Name: Anonymous 2008-06-08 15:53

>>10
0/10 not even any hint of effort in this

Name: Anonymous 2008-06-08 19:59

It's called a template. Learn C++, fool.

Name: Anonymous 2008-06-09 2:46

You can implement polymorphism of this kind in C, however:

1. YOU will have to implement handling of all possible types and the logic that chooses them.
2. Compiler will not help you -- you will have to pass the type as a value of some variable declared as enum.
3. If you were able to implement this properly, you wouldn't ask for it here.

Name: Anonymous 2008-06-09 5:27

use a union!

Name: Anonymous 2008-06-09 20:07

                       //`'''```,
             o        // JAVA   `.,
       ,....OOo.   .c;.',,,.'``.,,.`
    .'      ____.,'.//
   / _____  \___/.'
  | / ||  \\---\|
  ||  ||   \\  ||
  co  co    co co

Name: Anonymous 2008-06-09 20:12

its not hard at all. create a struct with a union of all of the types in c inside of it, and an enum to tell you which type it is

like so:


enum c_type {
  INT,
  SHORT,
  USHORT,
  ULONG,
  ...
};

struct type {
  enum c_type type;
  union {
    int a;
    short b;
    unsigned short c;
    unsigned long d;
    ...
  } storage;
};


then in your functions just do


if(var.type == INT)
  printf("%d\n", var.storage.a);
else if(var.type == CHAR)
  printf("%c\n", var.storage.z);

Name: Anonymous 2008-06-09 20:20

>>16
The lisp toad has defected?

Name: Anonymous 2008-06-09 20:22

>>18
It is now the Java toad.

Name: Anonymous 2008-06-09 22:28

>>17
What about compound types?

Also, SV*
Also, @encode()

Name: Anonymous 2009-03-06 5:54

Store So I figure   if I can   group them in   a JApplet Essentially?

Name: Anonymous 2011-01-31 21:20

<-- check em dubz

Name: Sgt.Kabukiman可姌 2012-05-23 15:13

All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy

Name: bampu pantsu 2012-05-29 3:44

bampu pantsu

Don't change these.
Name: Email:
Entire Thread Thread List