473,378 Members | 1,492 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

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 5041
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**********@boost-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_from_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_from_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
Victor Nazarov wrote:

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.


Yes, it is a mistake. It is a nonstandard extension.
Pointers to functions,
can be converted to pointers to other types of functions,
but (void *), is a pointer to an incomplete object type,
and is incompatible with function pointers.

--
pete
Nov 14 '05 #11

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

Similar topics

3
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...
0
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,...
13
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...
6
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...
50
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...
30
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
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
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...
14
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
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.