Link to home
Start Free TrialLog in
Avatar of guidway
guidwayFlag for United States of America

asked on

variant data type in C

I have a function that can return string/int/long/char/etc... which in most languages would be called a variant. However, I do not see that type in C, how would I return any type from a function?

thanks
guid
Avatar of Sjef Bosman
Sjef Bosman
Flag of France image

In most cases it is not possible. You can only return simple types from C. If you want to return more complex types, you have to use C++. Simple types are the standard machine-bound scalars, like words, long words, doubles, pointers, etc. Returning a string is (hence) impossible, but you can return a string pointer. Be aware of the fact that if you create a string in a local variable (i.e. on the stack), and you return a pointer to that string from a function, you're in for some trouble, because the stack might be overwritten!

What's your real problem, what do you want to accomplish?
string is not a type in C.its a char array.
can your function return only primitive data types or char arrays as well.

You could use a void pointer but you'd have to cast it manually.
If you know beforehand what kind of data is going to be returned then just returning pointers should allow you to achieve
something similar I guess.

How about a pointer to a Union here ?

Cheers.
Ankur...is your internet connection faster than mine :-))
Avatar of guidway

ASKER

sorry, I meant char* as a string, not an actual string type. didn't mean to confuse anyone.

>>can your function return only primitive data types or char arrays as well<<

it needs to return both primitive and char* (char pointers)

>>How about a pointer to a Union here ?<<

not familiar with Unions can you explain a little more? thanks
Will you know what data type is going to be returned when you are before you call the function ?
Avatar of guidway

ASKER

sorry missed this question:
>>What's your real problem, what do you want to accomplish?<<

Basically I am decrypting a binary file. I have VB code that I created to decrypt the file and I want to compare the speed differences between VB and C. In VB there is a function called readDataType that returns a variant which can be any type of data, I want to write the same function in C. does that explain anymore?
Avatar of guidway

ASKER

>>Will you know what data type is going to be returned when you are before you call the function ?<<

yes, luckily that will be one of the parameters passed in to the function. hopefully that makes solving this easier. :)
SOLUTION
Avatar of anupvijay
anupvijay
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Another way of solving the same problem is by returning a void* from your funtion and then type casting it
explicitly as per the expected return type.

That is what Ankratvb was talking about.

HTH
Cheers.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Stefan.......
A good idea that you gave.....that would allow the function to tell the caller what type it is returning..so the caller can afford
to not know it beforehand.
Thanks for that.....

Cheers.
Building on Anup's idea:

#include<stdio.h>
union d
{
 int a;
 float b;
};
typedef union d var;

var func(int d);
int main()
{
 var t;
 t=func(0);
 printf("%d",t.a);
 return 0;
}
var func(int d)
{
 var temp;
 if(d==1) temp.a=2;
 else temp.b=3.00;
 return temp;
}

Add stefan's idea and you'd have a pretty good solution.
>Ankur...is your internet connection faster than mine :-))

Yeah.I've got DSL. :~)))
What about you.
I will beat you there.......I have a high speed corporate connection (courtesy - my company) :-)

It must be just my typing speed :o))

Enjoy.
Avatar of guidway

ASKER

thanks everyone! I don't quite understand all of Stefan's example yet, but I'm pretty sure this will work for what I need. let me read up a little on unions first and make sure and I will close this either later today or tomorrow. thanks again :-)

guid
Woh...
Would i dare ask which company?

I just like to know what august company i am trying to be in. :~))
Here is an example with void pointers:

#include<stdio.h>
void *func(int d);
int main()
{
 int t;
 char c;
 t=*((int *)func(1));
 printf("%d",t);
 c=*((char *)func(2));
 printf("%c",c);

 return 0;
}
void * func(int d)
{
 void *temp;
 if(d==1)
  {
   int t=2;
   temp=&t;
  }
  else if(d==2)
  {
    char t='A';
    temp=&t;
  }
 return temp;
}
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of guidway

ASKER

understand it perfectly now (I think). :)

thanks again, increasing pts and splitting.

guid