guidway
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
thanks
guid
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.
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.
something similar I guess.
How about a pointer to a Union here ?
Cheers.
Ankur...is your internet connection faster than mine :-))
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
>>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 ?
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?
>>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?
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. :)
yes, luckily that will be one of the parameters passed in to the function. hopefully that makes solving this easier. :)
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
explicitly as per the expected return type.
That is what Ankratvb was talking about.
HTH
Cheers.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
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.
#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.
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.
It must be just my typing speed :o))
Enjoy.
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
guid
Woh...
Would i dare ask which company?
I just like to know what august company i am trying to be in. :~))
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;
}
#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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
understand it perfectly now (I think). :)
thanks again, increasing pts and splitting.
guid
thanks again, increasing pts and splitting.
guid
What's your real problem, what do you want to accomplish?