Solved

variant data type in C

Posted on 2004-04-22
21
969 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
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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
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 150 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 50 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 150 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 150 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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
logging Access violation 6 19
Picking random number 8 177
Convert image to byte array 8 201
Problem in finding output of a program 11 104
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
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 how to use strings and some functions related to them in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

772 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