Re: void* passed as funtion parameters?
- From: Robby <Robby@xxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Tue, 1 Sep 2009 07:50:03 -0700
Hello Ulrich,
2. You could also attach the type to the struct itself, like in the first
member an enumeration. This would allow you to distinguish the type easier.
You could then treat the void pointer as pointer to the enumeration, read
it, and then treat the different types accordingly.
This seems interesting, but I don't quite understand what you mean by:
"You could also attach the type to the struct itself, like in the first
member an enumeration."
I have never done this. Is it possible to show a very short example...
perhaps 5 lines or so, I would really appreciate it. If its too much trouble,
then its okay too.
Thankyou very much for your feedback.
I thank all who has responded to this post!
--
Best regards
Roberto
"Ulrich Eckhardt" wrote:
Robby wrote:.
[...stuff...]These three structs are still identical. Why again are you having three
of them?
Because the actual 3 structures in my real code are really:
===========================================#1
typedef struct tag_pc_table
{
long LKdct__F1;
long LKdct__F2;
long LKdct__F3;
}pc_table;[...other stuff...]
============================================#2
typedef struct tag_ddlb_table
{
long LKdct__F1;
long LKdct__F2;
long LKdct__F3;
} ddlb_table;[...more stuff...]
====================================#3
typedef struct tag_lb_table
{
long LKdct__F1;
long LKdct__F2;
long LKdct__F3;
}lb_table;
Unless I am totally out of it, I don't think they are all 3 identical!
Yeah, well, I hope you learn to not omit important details from your
examples.
Anyhow: The three share the same three initial members. You could factor out
a single base structure to use as first member and access them using this
base structure as type.
[three different structs, each containing a pointer/size combo to hold anAnd three more nearly identical structs. Why, oh why?
Because again, these other 3 structures in my real code, are really:
array of the above]
So, now what! Do we still feel that this would work:
void f1(void *x)
{
pc *p = x;
p->LK__F1 = 10;
Since 'pc' and the other two don't have LK__F1 [0] as common initial member,
this code doesn't work.
p->dc[0].LKdct__F1 = 20;
Note that while you can address the initial three members of the table
structs via any pointer to them, indexing into an array requires knowledge
of the size of the exact type, so this doesn't automatically work.
Igor, perhaps you are right as you usually are, but, I don't see how f1()
can access its respective members from the correct structure by simply
doing one cast as you show it above? Don't get me wrong here, I am not
going against you, its just that I sincerely don't see it.
You are right, it doesn't work. Now that you have shown the fill information
it is obvious that it doesn't work.
Some suggestions:
1. You could use a union type to store the array and store the array in the
initial members of the struct, so that you can factor out a base-struct
that contains the pointer/size_t combo.
2. You could also attach the type to the struct itself, like in the first
member an enumeration. This would allow you to distinguish the type easier.
You could then treat the void pointer as pointer to the enumeration, read
it, and then treat the different types accordingly.
3. You could also attach a function-pointer for various things that have
common behaviour but actually different implementations for different
types.
Up to now, there doesn't seem to be an easy way out.
Uli
[0] It is common agreement that using ALL_UPPERCASE should be left to
macros. It is a requirement of the standard that you don't use any names
with TWO__CONSECUTIVE underscores. Your program is technically ill-formed.
--
C++ FAQ: http://parashift.com/c++-faq-lite
Sator Laser GmbH
Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932
- Follow-Ups:
- Re: void* passed as funtion parameters?
- From: Ulrich Eckhardt
- Re: void* passed as funtion parameters?
- References:
- Re: void* passed as funtion parameters?
- From: Robby
- Re: void* passed as funtion parameters?
- From: Ulrich Eckhardt
- Re: void* passed as funtion parameters?
- Prev by Date: Re: void* passed as funtion parameters?
- Next by Date: Re: Process Name
- Previous by thread: Re: void* passed as funtion parameters?
- Next by thread: Re: void* passed as funtion parameters?
- Index(es):
Relevant Pages
|