• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1002
  • Last Modified:

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
0
guidway
Asked:
guidway
  • 7
  • 6
  • 5
  • +2
4 Solutions
 
Sjef BosmanGroupware ConsultantCommented:
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?
0
 
ankuratvbCommented:
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.
0
 
anupvijayCommented:
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.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
anupvijayCommented:
Ankur...is your internet connection faster than mine :-))
0
 
guidwayAuthor Commented:
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
0
 
anupvijayCommented:
Will you know what data type is going to be returned when you are before you call the function ?
0
 
guidwayAuthor Commented:
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?
0
 
guidwayAuthor Commented:
>>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. :)
0
 
anupvijayCommented:
For unions please visit

http://en.wikipedia.org/wiki/C_language_union


union {
int a;
float b;
} u;

If you have a union like the one above....

When you call the funtion you know what type you are gonna get back.
So call the function ....... fill the appropriate filed inside the funtion and read the same field once you have returned from the
funtion .......... But yes only possible if you know before hand which field to read and hence I asked you the same.

Try it out.......
Other experts any ideas other than this can chip in because this was just off my mind and I do not have the environment
to try it out......

Cheers.
0
 
anupvijayCommented:
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.
0
 
Sjef BosmanGroupware ConsultantCommented:
Well, in principle you can cast anything to anything in C (primitive types only). You can even convert a void * to a long if you want. So if you know what's coming, you can cast the result of your function to the type you need. Be careful with string (eh, char arrays).
0
 
stefan73Commented:
Hi anupvijay,
> union {
> int a;
> float b;
> } u;

Or you'd combine it in a struct with a field to indicate which type was returned:

struct s{
    enum{RETURN_INT,RETURN_FLOAT} type;
    union {
        int a;
        float b;
    } u;
};

gcc also allows anonymous unions:

struct s{
    enum{RETURN_INT,RETURN_FLOAT} type;
    union {
        int a;
        float b;
    };
};

...so there you can directly access the data of the union:

struct s my_s;

my_s.b = 1.5;



Cheers,
Stefan
0
 
anupvijayCommented:
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.
0
 
ankuratvbCommented:
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.
0
 
ankuratvbCommented:
>Ankur...is your internet connection faster than mine :-))

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

It must be just my typing speed :o))

Enjoy.
0
 
guidwayAuthor Commented:
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
0
 
ankuratvbCommented:
Woh...
Would i dare ask which company?

I just like to know what august company i am trying to be in. :~))
0
 
ankuratvbCommented:
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;
}
0
 
ankuratvbCommented:
Here is the example with stefan's idea:
It helps in that now the caller does not need to know what will be returned.

#include<stdio.h>
struct sdt
{
 enum{return_float,return_int} type;
 union
 {
 int a;
 float b;
 }d;
};
typedef struct sdt var;

var func(int d);
int main()
{
var t;
t=func(0);
if(t.type==return_float)
printf("%f",t.d.b);

t=func(1);
if(t.type==return_int)
printf("%d",t.d.a);

return 0;
}
var func(int d)
{
var temp;
if(d==1) {temp.d.a=2;temp.type=return_int;}
else {temp.d.b=3.00;temp.type=return_float;}
return temp;
}
0
 
guidwayAuthor Commented:
understand it perfectly now (I think). :)

thanks again, increasing pts and splitting.

guid
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 7
  • 6
  • 5
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now