Thread: Switch to different data types

  1. #1
    Registered User
    Join Date
    Jul 2013
    Posts
    7

    Switch to different data types

    Hi,

    I'm a newbie here, just registered and I need some help. Is there any way to make a switch in C. That I can choose between different data types? For example, at the moment I need to know how to read one parameter. It must recognize if it is a boolean or uint32. Can anyone help me? Tried to make myself clear as possible.

  2. #2
    Registered User
    Join Date
    Jul 2013
    Posts
    7
    This parameter value might be boolean or uint32. There is no idea what kind of a data type value it might be.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Welcome to the forum, Lauri!

    Post the code you are describing this problem with. Use [*code] tags [*/code] (without the *'s), around the code, so the forum software will display it very nicely as
    Code:
    code
    instead of regular forum text.

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    So what you're saying is: the parameter contains no information about how to determine it's type, yet from that lack of information, you wish to know how to determine it's type. That would be impossible, the function must receive some information regarding the type of the data.

    There are ways of making generic functions that can handle multiple types, however they usually require the caller to pass in two pieces of information (the type of data and the data itself), and rely on the caller getting the two parameters correct. This can be something like the format strings that printf uses; or you can pass in an enum value corresponding to the type of data you're passing in, along with the data; or you can build a struct that contains a type field, and a data field which is a union containing all the possible types you will use.

    I'm still not clear on exactly what it is you want, and I'm not at all sure why you would want to do it this way. Maybe you could provide some code along with an more detailed explanation.

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Sorry, but your question is unclear.

    When you say "read one parameter", what do you mean? Are you reading it from a file or stream (such as stdin)? If so, how is it represented in that file/stream? How would you recognise, if you were reading the file (or if you were the user typing input to stdin) whether it contained a boolean or an integral value?

    Computers, compilers, and software are not clairvoyant. If you can't describe in unambiguous terms what you mean, you can't expect a computer, compiler, or software to do what you intend.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Types
    By Sn0wcra5h in forum C Programming
    Replies: 1
    Last Post: 01-19-2010, 10:33 PM
  2. data types types...
    By gftmc in forum C++ Programming
    Replies: 3
    Last Post: 09-11-2006, 11:30 AM
  3. what types can you do switch on
    By Anddos in forum C++ Programming
    Replies: 15
    Last Post: 02-15-2006, 11:53 AM
  4. data types
    By §elfi~ in forum C++ Programming
    Replies: 2
    Last Post: 06-26-2003, 10:23 PM
  5. data types help
    By Bud in forum C Programming
    Replies: 2
    Last Post: 06-27-2002, 12:35 PM