Using a union as a function parameter

J

Juke All

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;
}
 
M

Mark A. Odell

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.
 
J

Juke All

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.


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).
 
M

Mark A. Odell

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.
 
M

Malcolm

Juke All said:
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)
(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.
 
M

Mark A. Odell

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);
 
D

Default User

Juke said:
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:


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
 
E

Eric Sosman

Mark said:
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 ..."
 
M

Mark A. Odell

Eric Sosman said:
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);
 
M

Martin Dickopp

Juke All said:
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
 
J

j

Eric Sosman said:
Mark said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top