473,804 Members | 2,112 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
10 6936
j

"Eric Sosman" <Er*********@su n.com> wrote in message
news:40******** ****@sun.com...
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 ..."


heh, this is gold. This has to be the most humorous post I have ever
read on c.l.c.
--
Er*********@sun .com


--
j
Nov 14 '05 #11

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

Similar topics

4
7614
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
3212
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
4619
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
2916
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
2586
by: steve.kim | last post by:
Hello, I'm trying to make a class like below... class myClass { public: // ctor / dtor .... // methods
1
2373
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
9594
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,...
0
10595
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10341
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
9171
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...
0
6862
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5530
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...
1
4308
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3831
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3001
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.