473,763 Members | 9,161 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Union type variable assignation --- in expression, in function argues

Hi, C lovers!

I stuck on an union problem

Here is snippet of my code

....

/* two pointers of function with repsectively one and two argues */
typedef int (*dce_sn_f)(dce _t*);
typedef int (*dce_io_f)(dce _t*, FILE*);

/* A union to store one of the previous declared type */
typedef union {
dce_sn_f sn; /* func with dce_t* argue */
dce_io_f io; /* func with dce_t* and FILE argues */
} dce_f;

....

typedef struct dce_slot_st {
dce_f f; /* may be dce_dn_t or dce_io_t */
const char *name;
} dce_slot_t;

....

static dce_slot_t *build_slot( dce_f fun, const char *name )
{
dce_slot_t *new_slot;

new_slot = utl_calloc( sizeof(dce_slot _t) );
if (new_slot) {
new_slot->f = fun; /* XXXXXX */
new_slot->name = name;

} else {
UTL_SYSERR( "cannot create new slot" );
return NULL;
}

return new_slot;

}

.....

dc->init = build_slot( stub_init, "stub_init" );
gcc detect an error on this last line and said:
" incompatible type for argument 1 of `build_slot' "

Does somedy show what is bad in my code

Does the expression (marked wih XXXXX in the code), look correct ? gcc
does not complain about it !

Thanks a lot

Denis
Nov 14 '05 #1
10 5076
Denis Pithon wrote:
dc->init = build_slot( stub_init, "stub_init" );

gcc detect an error on this last line and said:
" incompatible type for argument 1 of `build_slot' "

Does somedy show what is bad in my code


stub_init is undeclared.

--
pete
Nov 14 '05 #2
pete wrote:
Denis Pithon wrote:

dc->init = build_slot( stub_init, "stub_init" );

gcc detect an error on this last line and said:
" incompatible type for argument 1 of `build_slot' "

Does somedy show what is bad in my code

stub_init is undeclared.


Badly, stub_init is well declared ( just forget to copy it in the mail)

I obtain the same error with the simpler code below

#include <stdio.h>
#include <stdlib.h>
typedef int (*f1_t)(int);
typedef int (*f2_t)(int, char*);
typedef union {
f1_t f1;
f2_t f2;
} f_t;
typedef struct {
f_t fun;
const char *name;
} f_slot_t;
int my_func(int a)
{
return a;
}
f_slot_t *do_work( f_t fun, const char *name )
{
f_slot_t *f = calloc( 1, sizeof(f_slot_t ) );
f->fun = fun;
f->name = name;
return f;
}
int main()
{
f_slot_t *sl;
sl = do_work( my_func, "my_func" );
return 0;
}
Nov 14 '05 #3
Denis Pithon wrote:

pete wrote:
Denis Pithon wrote:
typedef union {
f1_t f1;
f2_t f2;
} f_t; int my_func(int a)
{
return a;
} f_slot_t *do_work( f_t fun, const char *name ) sl = do_work( my_func, "my_func" );

my_func isn't a union.
The first argument to do_work, should be a union.

--
pete
Nov 14 '05 #4
pete wrote:
Denis Pithon wrote:
pete wrote:
Denis Pithon wrote:

typedef union {
f1_t f1;
f2_t f2;
} f_t;


int my_func(int a)
{
return a;
}


f_slot_t *do_work( f_t fun, const char *name )


sl = do_work( my_func, "my_func" );


my_func isn't a union.
The first argument to do_work, should be a union.


I compile the code with
gcc -Wall -W -pedantic union.c

I just found that if remove -pedantic (eventually replace by -ansi), all
seems to be fine, if I cast my_func to (f_t) type, ie:

sl = do_work( (f_t) my_func, "my_func" );

compile quietly with: gcc -Wall -W -ansi union.c

If i keep -pedantic flag, gcc tell me that "ISO C forbids casts to union
type"...

So, I have to wrote as many do_work functions that I have type in the
union ???

Is there another way to it ?
Nov 14 '05 #5
"Denis Pithon" <de**********@b oost-technoogies.com > wrote in message
news:40******** **************@ news.free.fr...
pete wrote:
Denis Pithon wrote:
pete wrote:

Denis Pithon wrote:

typedef union {
f1_t f1;
f2_t f2;
} f_t;


int my_func(int a)
{
return a;
}


f_slot_t *do_work( f_t fun, const char *name )


sl = do_work( my_func, "my_func" );


my_func isn't a union.
The first argument to do_work, should be a union.


I compile the code with
gcc -Wall -W -pedantic union.c

I just found that if remove -pedantic (eventually replace by -ansi), all
seems to be fine, if I cast my_func to (f_t) type, ie:

sl = do_work( (f_t) my_func, "my_func" );

compile quietly with: gcc -Wall -W -ansi union.c

If i keep -pedantic flag, gcc tell me that "ISO C forbids casts to union
type"...

So, I have to wrote as many do_work functions that I have type in the
union ???

Is there another way to it ?


The problem is that you are trying to pass
the ADDRESS OF A FUNCTION as parameter that
expects a union aggregate.

You should change the function parameter to
accept a pointer to the union, rather than
the union itself. Then allocate the union
somewhere and stuff the function pointer
into that union. Then pass the address of
the union to your do_work function.

Turn the warnings back on. They are telling
you that you screwed up big time. Put in the
suggested fixes and turn on the warnings.
--
----------------------------
Jeffrey D. Smith
Farsight Systems Corporation
24 BURLINGTON DRIVE
LONGMONT, CO 80501-6906
http://www.farsight-systems.com
z/Debug debugs your Systems/C programs running on IBM z/OS!
Are ISV upgrade fees too high? Check our custom product development!
Nov 14 '05 #6
Denis Pithon wrote:

pete wrote:
Denis Pithon wrote:

dc->init = build_slot( stub_init, "stub_init" );

gcc detect an error on this last line and said:
" incompatible type for argument 1 of `build_slot' "

Does somedy show what is bad in my code

stub_init is undeclared.


Badly, stub_init is well declared ( just forget to copy it in the mail)

I obtain the same error with the simpler code below

#include <stdio.h>
#include <stdlib.h>
typedef int (*f1_t)(int);
typedef int (*f2_t)(int, char*);
typedef union {
f1_t f1;
f2_t f2;
} f_t;
typedef struct {
f_t fun;
const char *name;
} f_slot_t;
int my_func(int a)
{
return a;
}
f_slot_t *do_work( f_t fun, const char *name )
{
f_slot_t *f = calloc( 1, sizeof(f_slot_t ) );
f->fun = fun;
f->name = name;
return f;
}
int main()
{
f_slot_t *sl;
sl = do_work( my_func, "my_func" );
return 0;
}

I couldn't help but notice, that unions don't
seem to have anything to do with your posted code.
Your function doesn't make any use of a union type parameter,
and you don't have any unions available outside the function,
to pass as arguments. "fun" and "name", are structure members.

--
pete
Nov 14 '05 #7
Denis Pithon wrote:
pete wrote:
Denis Pithon wrote:
pete wrote:

Denis Pithon wrote:



typedef union {
f1_t f1;
f2_t f2;
} f_t;


int my_func(int a)
{
return a;
}


f_slot_t *do_work( f_t fun, const char *name )


sl = do_work( my_func, "my_func" );


You must use something like:

sl = do_work(cast_fr om_f1_to_f (my_func), "my_func" );

where:

f_t cast_from_f1_to _f (f1_t f1)
{
f_t f;

return f.f1 = f1;
}

Another question is how you detect wich member of the union contain
correct data. In unions only one member contain correct data (there are
some exeptions, see standart), so you must store some additional info
about what member to use.

In your case you can use void * type instead of union.

typedef void *f_t

/* ... */

sl = do_work ((void *)my_func, "my_func");

Cast is needed there because standart doesn't allow explicit cast to
pointers to functions.

Nov 14 '05 #8
Victor Nazarov wrote:

Denis Pithon wrote:
> pete wrote:
>
>> Denis Pithon wrote:
>>
>>> pete wrote:
>>>
>>>> Denis Pithon wrote:
>>
>>
>>
>>
>>
>>> typedef union {
>>> f1_t f1;
>>> f2_t f2;
>>> } f_t;
>>
>>
>>
>>
>>> int my_func(int a)
>>> {
>>> return a;
>>> }
>>
>>
>>
>>
>>> f_slot_t *do_work( f_t fun, const char *name )
>>
>>
>>
>>
>>> sl = do_work( my_func, "my_func" );


You must use something like:

sl = do_work(cast_fr om_f1_to_f (my_func), "my_func" );

where:

f_t cast_from_f1_to _f (f1_t f1)
{
f_t f;

return f.f1 = f1;
}

Another question is how you detect wich member of the union contain
correct data. In unions only one member contain correct data (there are
some exeptions, see standart), so you must store some additional info
about what member to use.

In your case you can use void * type instead of union.

typedef void *f_t

/* ... */

sl = do_work ((void *)my_func, "my_func");

Cast is needed there because standart doesn't allow explicit cast to
pointers to functions.


I don't understand what you're saying.
You can't cast a function pointer to (void *),
therfore cast is not allowed there.

--
pete
Nov 14 '05 #9
pete wrote:
Victor Nazarov wrote:
I don't understand what you're saying.
You can't cast a function pointer to (void *),
therfore cast is not allowed there.


I've ment you need cast-operator in expression
p = (void *)my_func
where p was defined as void *;
I really don't know what standart says about this assignment, but that
has worked on my implementation so I could lately cast p back to pinter
to function. So I've just spread the idea of ALMOSTANYTYPE *
representation as void * to pointers to functions.
I want to know if that was a misstake. So I'm sorry if it was.
Sorry for my english.

Vir

Nov 14 '05 #10

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

Similar topics

3
1565
by: Sid | last post by:
Hi folks, I wrote this code to test if I get the same address for all the variables in a union but for some reason the address I got when I used a char variable in a union seems bizarre- can anyone help explain why I get a bizzarre address for a char variable inside a union ? In other words why is &(x.i) not 0012FED0 in the code below ?
0
3959
by: s_gregory | last post by:
The mdb is considerable size 70 +- mb. A complex union query was working well, but when an additional union select... was added into the query, selecting identical fields from a different source, it began to have problems. Failure was observed only in a few PC's at first. For example, in an NT 4.0 SP6 PC, it continued to work OK. But in my Win 2k laptop, it failed. As the union query was gradually simiplified in testing, the failure...
13
12344
by: Razmig K | last post by:
Dear mates, This is an another survey for the common (and uncommon) nontrivial uses of the aforementioned C construct. It's posted by the same average C programmer who's made a similar survey about the uses of 'enum's. I know that space optimization is the key idea behind 'union's, and in most cases a type-field is necessary to verify the type of the entity in the 'union' variable.
6
3335
by: Neil Zanella | last post by:
Hello, I would like to know what the C standards (and in particular the C99 standard) have to say about union initializers with regards to the following code snippet (which compiles fine under gcc 3.2.2 but does not produce the expected results, the expected results being the ones annotated in the comments in the code): #include <stdlib.h>
50
6499
by: Mikhail Teterin | last post by:
Hello! The sample program below is compiled fine by gcc (with -Wall), but rejected by Sun's SUNWspro compiler (version 6 update 2). The point of contention is, whether a value for one of the union's types can be passed to a function directly -- without creating a separate variable of the union type and assigning the appropriate field of it. Is gcc being too liberal, or is this behavior simply part of a newer
30
3284
by: Yevgen Muntyan | last post by:
Hey, Why is it legal to do union U {unsigned char u; int a;}; union U u; u.a = 1; u.u; I tried to find it in the standard, but I only found that
4
13838
by: Theo R. | last post by:
Hi all, I have the following struct defined - #define INTEGER 0 #define STRING 1 typedef struct { char type ; union {
29
2199
by: Richard Harter | last post by:
There is probably a simple way to do what I want but I don't see it. Any suggestions are welcome. Suppose I have a function foo with an argument that can be any of several types and that I want to use a union for that argument. In a header file there is the following: #define VAL_ALT union urt_value_alt .....
14
28154
by: deepak | last post by:
Hi Experts, I'm getting this compilation error while trying to access a member in structure. at what time we will get this error message? Thanks, Deepak
0
9387
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
10148
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...
0
10002
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9938
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
8822
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
7368
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
6643
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();...
1
3917
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
3528
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.