Re: void* passed as funtion parameters?

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



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:
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;
[...stuff...]
}pc_table;

============================================#2
typedef struct tag_ddlb_table
{
long LKdct__F1;
long LKdct__F2;
long LKdct__F3;
[...other stuff...]
} ddlb_table;

====================================#3
typedef struct tag_lb_table
{
long LKdct__F1;
long LKdct__F2;
long LKdct__F3;
[...more stuff...]
}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.

And three more nearly identical structs. Why, oh why?

Because again, these other 3 structures in my real code, are really:
[three different structs, each containing a pointer/size combo to hold an
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

.



Relevant Pages

  • Re: Size of an arraylist in bytes
    ... the pointer is an object. ... struct point make_point ... the structure or union contains a member with array type ... and temporary lifetime. ...
    (comp.lang.c)
  • Re: Yet another binary search tree library
    ... type is 'struct my_struct) containing the member 'my_node' pointed ... The pointer conversions ... and less assuming reading ...
    (comp.lang.c)
  • Re: Size of an arraylist in bytes
    ... the pointer is an object. ... struct point make_point ... member, then it's still covered by the old rules which don't imply ... expression is an lvalue. ...
    (comp.lang.c)
  • Re: Requesting sample code!
    ... a function can never be a member of a struct. ... Putting a pointer to function in a structure is no different. ... than a pointer to int or pointer to char or pointer to struct. ...
    (microsoft.public.vc.language)
  • Re: What do others make of this code?
    ... The intent of the 'm_' is to distinguish the variable as a member ... I also didn't like the "m_" prefix. ... C++ programming guidelines. ... It all depends IMHO on how the struct is used. ...
    (comp.lang.cpp)