Actually I want to have a variable which will receive every kind of value whether it is int or float or char. Can anyone please tell me a way to do this. But the program should be in C (not in C++).
-
The best practice depends on how you will use them and in what way the processing differs depending on the variable type. – Rerito Apr 09 '13 at 12:04
-
Consider implementing this part of your application in a dynamic-typed programming language, and its performance-critical part in C. It may be hard *at first* to mix different programming languages for a single application, but when you have learned it, it will be a real bless. – Alex Shesterov Apr 09 '13 at 12:18
5 Answers
You should always use the type that makes sense for your application, but if you really need a "typeless" data element, you can do that with a void *
and then use it by typecasting to whatever you're passing it:
void *myvar = malloc(100); // set up typeless element
*(int *)myvar = 10; // cast to an int and save an int
printf("%d\n", *(int *)myvar); // print as an int
*(float *)myvar = 10.25; // cast to a float and save a float
printf("%f\n", *(float *)myvar); // print as a float

- 47,263
- 29
- 113
- 177
-
2Probably better to use `max(sizeof(int), sizeof(float))` than an arbitrary 100. – James M Apr 09 '13 at 12:03
-
Hi Mike, Thnx 4 ur response. I also know that it can be done via void pointer and then doing the typecast but what I want is to have input of any kind in a linked list data without using the void pointer in c.The question what was asked to me was like you need to have any kind of input whether it is int or float or char in linked list without knowing it's type before and another condition was not to use void pointer. – Mayank Apr 10 '13 at 04:51
-
@Mayank - with literally **no** knowledge of type (meaning just save "anything" to a variable) there is no way to do this in C. You need to typecast or set a flag that indicates what the type is. – Mike Apr 10 '13 at 12:16
Your question has two answers. One is using unions(if you are restricted to int,char and float only). The other is deadly void * type. None of them is good coding practice. May be you should change the question :)
union {
int i;
char c;
float f;
} u;

- 1,697
- 1
- 12
- 20
-
2
-
1according to jsf coding standards(4.20) and MISRA unions should be banned. also void * removes type safety and you are prone to type errors after words. though in c you need to use them in some specific cases – Gorkem Apr 09 '13 at 12:04
-
Hi Beehorf, Thnx 4 ur response. I also know that it can be done via void pointer and then doing the typecast but what I want is to have input of any kind in a linked list data without using the void pointer in c.The question what was asked to me was like you need to have any kind of input whether it is int or float or char in linked list without knowing it's type before and another condition was not to use void pointer. – Mayank Apr 10 '13 at 04:58
C is a strong statically typed language, therefore you can't have a dynamic variable in the sense you're asking. I guess you could have a void pointer (void*) to point to anything, but it's really not a recommended approach (how would you know what to dereference it as?).

- 166
- 1
- 3
-
Hi Utopia, Thnx 4 ur response. I also know that it can be done via void pointer and then doing the typecast but what I want is to have input of any kind in a linked list data without using the void pointer in c.The question what was asked to me was like you need to have any kind of input whether it is int or float or char in linked list without knowing it's type before and another condition was not to use void pointer. – Mayank Apr 10 '13 at 04:59
-
As I mentioned, C is a strong statically typed language, which means that it requires knowledge of each variables type at compile time. Other people have mentioned using union, which is probably the best solution given your requirements, but you still need knowledge of what datatype it is actually storing to extract it correctly. You could store as a struct with a union for the data and a flag for the type, then parse that flag when getting the data and retrieve the correct value, but it results in significant code bloat for each retrieval. – Utopia Apr 10 '13 at 10:22
Read the input as a text string (array of chars). Then analyze it:
- is it a valid sequence of characters for an integer? If so, convert it into an integer (whichever can fit it:
signed char
,short
,int
,long
,long long
or theunsigned
variety of the same) - is it a valid sequence of characters for a floating-point number? If so, convert it
float
ordouble
orlong double
) - otherwise it's a string (unless you want to have a single character type)
Once done, place the converted value into the appropriate member of the union and set the type:
typedef struct
{
int Type; // tells which one of the below holds the value
union
{
signed char sc;
unsigned char uc;
short s;
unsigned short us;
int i;
unsigned ui;
long l;
unsigned long ul;
long long ll;
unsigned long long ull;
float f;
double d;
long double ld;
char* str;
char ch;
} Value;
} tValue;
Note that what looks like an integer may not fit into the biggest integer type and in that case you have an option of converting it into a floating point value, possibly with loss of precision. Otherwise you may say it's an error and handle it appropriately.
Also, when converting floating point numbers you should probably begin with larger types first (long double
) and then try the next smaller (double
and then float
) to see if the value is the same in both cases, in which case you can choose the smaller type.

- 61,140
- 12
- 83
- 180
-
Hi Alexey, Thnx 4 ur response. I also know that it can be done via void pointer and then doing the typecast but what I want is to have input of any kind in a linked list data without using the void pointer in c.The question what was asked to me was like you need to have any kind of input whether it is int or float or char in linked list without knowing it's type before and another condition was not to use void pointer. – Mayank Apr 10 '13 at 04:57
-
use a void pointer to store the address of the variable and typecast to get it back for 'every' type. Union you still have to know beforehand what are the types you are going to use. to use in macro, look for gcc typeof

- 2,454
- 19
- 22
-
Hi Abasu, Thnx 4 ur response. I also know that it can be done via void pointer and then doing the typecast but what I want is to have input of any kind in a linked list data without using the void pointer in c.The question what was asked to me was like you need to have any kind of input whether it is int or float or char in linked list without knowing it's type before and another condition was not to use void pointer. – Mayank Apr 10 '13 at 05:01