Datatype

This is a discussion on Datatype within the C Programming forums, part of the General Programming Boards category; Hello all, I was just wondering if there is anyway to find, what datatype a variable is? I mean like ...

  1. #1
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682

    Datatype

    Hello all, I was just wondering if there is anyway to find, what datatype a variable is? I mean like without using the sizeof operator. May be sometimes even the sizeof operator wouldn't be like more helpful at all in finding what datatype a variables is, depends on what compiler used and stuff.

    But I was just wondering if we could. I tried it all possible way. Well at least, to my knowledge.

    Thanks for your help guys

    ssharish

  2. #2
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    If you wrote it, you should know what the datatype is.
    If you mean you're getting a void* and you want to find out what it was before it was cast to void*, then I think you're out of luck. Switch to C++ and use templates in that case.

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    GCC has typeid.

  4. #4
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by robwhit View Post
    GCC has typeid.
    typeid is only for C++ though isn't it?

  5. #5
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    Quote Originally Posted by cpjust View Post
    typeid is only for C++ though isn't it?
    True, the typeid is used only in C++.

    ssharish

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Perhaps you can provide some more explanation of what you actually want to achieve.

    For purposes of macros etc, you can (in gcc) use typeof(x), where x is some variable declared earlier in the code. But for most things, you shouldn't really have to care - you should KNOW.

    There are cases where you would want to pass some "could cointain anything" type of data - in which case a struct and union combination would be the correct solution.

    But without understanding your problem better, I don't think it's possible to suggest a correct solution.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Yeah that's what I was thinking of. typeof.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing back objects from with functions. Datatype?
    By Swerve in forum C++ Programming
    Replies: 3
    Last Post: 03-20-2009, 12:50 PM
  2. arbitrary datatype arguments
    By eshwar_gunner in forum C Programming
    Replies: 4
    Last Post: 03-03-2009, 11:30 PM
  3. datatype bit?
    By MK27 in forum C Programming
    Replies: 3
    Last Post: 02-05-2009, 01:56 PM
  4. C Math datatype Help
    By OneDaHardWay in forum C Programming
    Replies: 4
    Last Post: 12-03-2005, 05:33 PM
  5. C Datatype WCHAR
    By GUI_XP in forum C Programming
    Replies: 2
    Last Post: 11-29-2002, 06:24 PM

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