473,796 Members | 2,654 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Using a union as a function parameter

When I compile the code (below), I get this error:
cannot convert parameter 1 from 'int' to 'union dna'

Without saying:

FOO x;
x.val = 100;

....is it possible to use a union as a function parameter, and when
calling that function, pass the argument as one of the types of the
union (int, in the following case)?
typedef union foo
{
void* ptr;
int val;
} FOO;

void* test(FOO obj)
{
void* ptr = obj.ptr;
int val = obj.val;
return NULL;
}

int main(int argc, char* argv[])
{
int x = 100;
test(x);
return 0;
}

Nov 14 '05 #1
10 6930
Juke All <me@here.net> wrote in
news:ai******** *************** *********@4ax.c om:
When I compile the code (below), I get this error:
cannot convert parameter 1 from 'int' to 'union dna'

Without saying:

FOO x;
x.val = 100;

...is it possible to use a union as a function parameter, and when
calling that function, pass the argument as one of the types of the
union (int, in the following case)?
typedef union foo
{
void* ptr;
int val;
} FOO;

void* test(FOO obj)
{
void* ptr = obj.ptr;
int val = obj.val;
return NULL;
}

int main(int argc, char* argv[])
{
int x = 100;
test(x);


What the ^*&^& are you thinking here? You told the compiler that test()
takes a FOO as a parameter and then you give it a stinking int? What part
of the compiler error isn't clear? Try declaring 'x' as type FOO and try
again.

--
- Mark ->
--
Nov 14 '05 #2
On 27 May 2004 21:05:15 GMT, "Mark A. Odell" <od*******@hotm ail.com>
wrote:
Juke All <me@here.net> wrote in
news:ai******* *************** **********@4ax. com:
When I compile the code (below), I get this error:
cannot convert parameter 1 from 'int' to 'union dna'

Without saying:

FOO x;
x.val = 100;

...is it possible to use a union as a function parameter, and when
calling that function, pass the argument as one of the types of the
union (int, in the following case)?
typedef union foo
{
void* ptr;
int val;
} FOO;

void* test(FOO obj)
{
void* ptr = obj.ptr;
int val = obj.val;
return NULL;
}

int main(int argc, char* argv[])
{
int x = 100;
test(x);


What the ^*&^& are you thinking here? You told the compiler that test()
takes a FOO as a parameter and then you give it a stinking int? What part
of the compiler error isn't clear? Try declaring 'x' as type FOO and try
again.

--
- Mark ->

I know that works, hence, the part of my message where I said:

Without saying:
FOO x;
x.val = 100;

I was just wondering if it was possible to pass an int into something
that expects union foo (and one of the union members is an int).
Nov 14 '05 #3
Juke All <me@here.net> wrote in
news:tc******** *************** *********@4ax.c om:
void* test(FOO obj)
{
void* ptr = obj.ptr;
int val = obj.val;
return NULL;
}

int main(int argc, char* argv[])
{
int x = 100;
test(x);
What the ^*&^& are you thinking here? You told the compiler that test()
takes a FOO as a parameter and then you give it a stinking int? What
part of the compiler error isn't clear? Try declaring 'x' as type FOO
and try again.

I know that works, hence, the part of my message where I said:

Without saying:
FOO x;
x.val = 100;

I was just wondering if it was possible to pass an int into something
that expects union foo (and one of the union members is an int).


Of course not! That's the whole point of a typed language. If you want to
save some typing you can initialize x at the point of definition like
this:

int main(void)
{
FOO x = { NULL, 100 };
test(x);

return 0;
}

This probably won't do what you want though. Also, realize you are passing
a copy of x into test which is fine but if you expect test() to modify x
for use by main() you will need to change test() to take a FOO * instead.

--
- Mark ->
--
Nov 14 '05 #4

"Juke All" <me@here.net> wrote in message

I was just wondering if it was possible to pass an int into something
that expects union foo (and one of the union members is an int).

If union foo and int are the same size you might find a way of subverting
the compiler's type system to do this, but what's the point?

BTW this code

void* test(FOO obj)
{
void* ptr = obj.ptr;
int val = obj.val;
return NULL;
}


(obj.val and obj.ptr share the same storage) is illegal - one may not write
a member of a union as one type and read it as a second. This is often
breached by platform-specific extensions, but is an ANSI rule.

Nov 14 '05 #5
"Mark A. Odell" <od*******@hotm ail.com> wrote in
news:Xn******** *************** *********@130.1 33.1.4:
Juke All <me@here.net> wrote in
news:tc******** *************** *********@4ax.c om:
void* test(FOO obj)
{
void* ptr = obj.ptr;
int val = obj.val;
return NULL;
}

int main(int argc, char* argv[])
{
int x = 100;
test(x);

What the ^*&^& are you thinking here? You told the compiler that
test() takes a FOO as a parameter and then you give it a stinking int?
What part of the compiler error isn't clear? Try declaring 'x' as type
FOO and try again.

I know that works, hence, the part of my message where I said:

Without saying:
FOO x;
x.val = 100;

I was just wondering if it was possible to pass an int into something
that expects union foo (and one of the union members is an int).


Of course not! That's the whole point of a typed language. If you want
to save some typing you can initialize x at the point of definition like
this:

int main(void)
{
FOO x = { NULL, 100 };


eh-hem, I was thinking struct here. Ignore this post please. You can force
the compiler to accept the int as a FOO via:

int x = 100;

test((FOO) x);

--
- Mark ->
--
Nov 14 '05 #6
Mark A. Odell wrote:
"Mark A. Odell" <od*******@hotm ail.com> wrote in
news:Xn******** *************** *********@130.1 33.1.4:

Juke All <me@here.net> wrote in
news:tc****** *************** ***********@4ax .com:

>void* test(FOO obj)
>{
> void* ptr = obj.ptr;
> int val = obj.val;
> return NULL;
>}
>
>int main(int argc, char* argv[])
>{
> int x = 100;
> test(x);

What the ^*&^& are you thinking here? You told the compiler that
test() takes a FOO as a parameter and then you give it a stinking int?
What part of the compiler error isn't clear? Try declaring 'x' as type
FOO and try again.

I know that works, hence, the part of my message where I said:

Without saying:
FOO x;
x.val = 100;

I was just wondering if it was possible to pass an int into something
that expects union foo (and one of the union members is an int).


Of course not! That's the whole point of a typed language. If you want
to save some typing you can initialize x at the point of definition like
this:

int main(void)
{
FOO x = { NULL, 100 };

eh-hem, I was thinking struct here. Ignore this post please. You can force
the compiler to accept the int as a FOO via:

int x = 100;

test((FOO) x);


Umpire: "Steee-riiike two!"

Commentator: "Another bad swing at a pitch 'way outside
the strike zone! That's not the Odell we're used to; something
about this pitcher's dipsy-doodle side-arm delivery has got
him baffled. Let's hope he gets un-baffled -- he digs in,
looks out at the mound, here's the windup ..."

--
Er*********@sun .com

Nov 14 '05 #7
Eric Sosman <Er*********@su n.com> wrote in news:40******** ****@sun.com:
int main(void)
{
FOO x = { NULL, 100 };

eh-hem, I was thinking struct here. Ignore this post please. You can
force the compiler to accept the int as a FOO via:

int x = 100;

test((FOO) x);


Umpire: "Steee-riiike two!"

Commentator: "Another bad swing at a pitch 'way outside
the strike zone! That's not the Odell we're used to; something
about this pitcher's dipsy-doodle side-arm delivery has got
him baffled. Let's hope he gets un-baffled -- he digs in,
looks out at the mound, here's the windup ..."


See, this is what happens when I try to think "bad". I simply cannot do it
on purpose correctly. I do it fine accidentally, however. I'll try one
more time to work around the C type system even though I wouldn't do this
at home...

int x = 100;

test(*(FOO *) &x);

--
- Mark ->
--
Nov 14 '05 #8
Juke All wrote:
I was just wondering if it was possible to pass an int into something
that expects union foo (and one of the union members is an int).

Even if you could, the function that was called wouldn't know which had
been set. Your function here:

void* test(FOO obj)
{
void* ptr = obj.ptr;
int val = obj.val;
return NULL;
}


would be dicey.
[#5] With one exception, if the value of a member of a union
object is used when the most recent store to the object was
to a different member, the behavior is
implementation-defined.
What is it you are trying to do? Tell us your problem, not your broken
solution.


Brian Rodenborn
Nov 14 '05 #9
Juke All <me@here.net> writes:
When I compile the code (below), I get this error:
cannot convert parameter 1 from 'int' to 'union dna'

Without saying:

FOO x;
x.val = 100;

...is it possible to use a union as a function parameter, and when
calling that function, pass the argument as one of the types of the
union (int, in the following case)?
In C89, you cannot do that without using a temporary union object. In
C99, this works:

int x = 100;
test ((FOO){.val = x});
typedef union foo
{
void* ptr;
int val;
} FOO;

void* test(FOO obj)
{
void* ptr = obj.ptr;
If the `val' member of `obj' has been initialized, it results in
undefined behavior to access the `ptr' member.
int val = obj.val;
return NULL;
}

int main(int argc, char* argv[])
{
int x = 100;
test(x);
return 0;
}


Martin
--
,--. Martin Dickopp, Dresden, Germany ,= ,-_-. =.
/ ,- ) http://www.zero-based.org/ ((_/)o o(\_))
\ `-' `-'(. .)`-'
`-. Debian, a variant of the GNU operating system. \_/
Nov 14 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
7612
by: jonnychang | last post by:
What is the purpose of using union in this class? Class A { public: union { int age; char * name; double amount; } };
1
3211
by: Alex Vinokur | last post by:
========================= Windows 2000 Professional Digital Mars C/C++ 8.36 STLport 4.5.3 ========================= I have got a problem with compilation of the following piece of code using Digital Mars C/C++ compiler. There was no problem with this code while using GNU g++ 3.2.
3
4618
by: Rich Protzel | last post by:
Hello, So my table contains say 100,000 records, and I need to group the categories in fld1 by the highest count of subcategories. Say fld1 contains categories A, B, C, D, E. All of these categories contain subcategories AA, AB, AC, AD,...AJ, BA, BB...BJ, CA, CB, CC...CJ, etc in fld2. I am counting how many subcategories are listed for each category. Like
10
1793
by: rg | last post by:
Hi all, I was wondering if anyone had dealt with a similar problem. I need to use a template function as the parameter for a particular function (also template function). The program compiles into an object file but then at the final stage it says that it can't find template function. The platform is WindowsXP Pro, MSCV++ ..Net. More specifically what I want to do is write an atl algorithm that will also
6
1971
by: komal | last post by:
hi all basically my problem is i have to write a function such that when ever i call this function in some other function .it should give me tha data type and value of calling function parameter.and no of parameter is calling function can be anything. for example.suppose my function is function2. then when i call function1(int i ,char j,float d) { function2()
12
2915
by: Susan Bricker | last post by:
For those of you who have been following my posts - they all pertain to a Dog Competition Organization's Database. There are three classes that the dogs can participate: NOVICE, OPEN, and UTILITY. I want to produce a report of the top 10 average scores for each class for each year.
16
3167
by: hzmonte | last post by:
Correct me if I am wrong, declaring formal parameters of functions as const, if they should not be/is not changed, has 2 benefits; 1. It tells the program that calls this function that the parameter will not be changed - so don't worry. 2. It tells the implementor and the maintainer of this function that the parameter should not be changed inside the function. And it is for this reason that some people advocate that it is a good idea to...
6
2584
by: steve.kim | last post by:
Hello, I'm trying to make a class like below... class myClass { public: // ctor / dtor .... // methods
1
2372
by: vekka | last post by:
Hi! Programming language: C I wondered if any of you are able to see what is wrong with my code. What I want to do is to find the union of a set of numbers. I use a singly linked list. and my union function is based on a simple merge algorithm. There are four different sets: even numbers, odd numbers, prime numbers and non prime numbers. When I run my program now, it prints out: Even or Odd numbers: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 .. 50 ...
0
9679
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9527
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10172
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10003
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9050
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7546
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5441
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5573
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2924
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.