?
Solved

variant data type in C

Posted on 2004-04-22
21
Medium Priority
?
983 Views
Last Modified: 2010-04-15
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
Comment
Question by:guidway
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 6
  • 5
  • +2
21 Comments
 
LVL 46

Expert Comment

by:Sjef Bosman
ID: 10889504
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
 
LVL 9

Expert Comment

by:ankuratvb
ID: 10889517
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
 
LVL 2

Expert Comment

by:anupvijay
ID: 10889536
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 2

Expert Comment

by:anupvijay
ID: 10889542
Ankur...is your internet connection faster than mine :-))
0
 
LVL 12

Author Comment

by:guidway
ID: 10889768
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
 
LVL 2

Expert Comment

by:anupvijay
ID: 10889812
Will you know what data type is going to be returned when you are before you call the function ?
0
 
LVL 12

Author Comment

by:guidway
ID: 10889845
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
 
LVL 12

Author Comment

by:guidway
ID: 10889880
>>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
 
LVL 2

Assisted Solution

by:anupvijay
anupvijay earned 600 total points
ID: 10889989
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
 
LVL 2

Expert Comment

by:anupvijay
ID: 10890020
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
 
LVL 46

Assisted Solution

by:Sjef Bosman
Sjef Bosman earned 200 total points
ID: 10890024
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
 
LVL 12

Assisted Solution

by:stefan73
stefan73 earned 600 total points
ID: 10890065
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
 
LVL 2

Expert Comment

by:anupvijay
ID: 10890114
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
 
LVL 9

Expert Comment

by:ankuratvb
ID: 10890133
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
 
LVL 9

Expert Comment

by:ankuratvb
ID: 10890293
>Ankur...is your internet connection faster than mine :-))

Yeah.I've got DSL. :~)))
What about you.
0
 
LVL 2

Expert Comment

by:anupvijay
ID: 10890315
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
 
LVL 12

Author Comment

by:guidway
ID: 10890329
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
 
LVL 9

Expert Comment

by:ankuratvb
ID: 10890361
Woh...
Would i dare ask which company?

I just like to know what august company i am trying to be in. :~))
0
 
LVL 9

Expert Comment

by:ankuratvb
ID: 10890524
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
 
LVL 9

Accepted Solution

by:
ankuratvb earned 600 total points
ID: 10890626
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
 
LVL 12

Author Comment

by:guidway
ID: 10892150
understand it perfectly now (I think). :)

thanks again, increasing pts and splitting.

guid
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.
Suggested Courses
Course of the Month11 days, 14 hours left to enroll

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question