Solved

variant data type in C

Posted on 2004-04-22
21
960 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
  • 7
  • 6
  • 5
  • +2
21 Comments
 
LVL 46

Expert Comment

by:Sjef Bosman
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 2

Expert Comment

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

Author Comment

by:guidway
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
>>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 150 total points
Comment Utility
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
Comment Utility
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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 46

Assisted Solution

by:Sjef Bosman
Sjef Bosman earned 50 total points
Comment Utility
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 150 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
>Ankur...is your internet connection faster than mine :-))

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

Expert Comment

by:anupvijay
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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 150 total points
Comment Utility
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
Comment Utility
understand it perfectly now (I think). :)

thanks again, increasing pts and splitting.

guid
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.

771 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now