Re: void* passed as funtion parameters?
Hello Igor,
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;
long e__pc_xchoice[PC_XCHO];
long e__pc_root_field[PC_ROOT];
int e__field1[PC_DF1];
int e__field2[PC_DF2];
int e__field3[PC_DF3];
int e__field4[PC_DF4];
long list_id;
char next_wnd_proc[PC_FWP][CHARS_MAX];
}pc_table;
============================================#2
typedef struct tag_ddlb_table
{
long LKdct__F1;
long LKdct__F2;
long LKdct__F3;
short quit_flag;
long message;
int l_mbs;
int h_mbe;
long e__ddlb_xchoice[DDLB_XCHO];
long ddlb_root_field[DDLB_ROOT];
long *e__ll;
int list_id;
long long_list_lenght;
int fwp[3];
char next_wnd_proc[DDLB_FWP][CHARS_MAX];
} ddlb_table;
====================================#3
typedef struct tag_lb_table
{
long LKdct__F1;
long LKdct__F2;
long LKdct__F3;
long TITLE;
int Q_MBS;
int Q_MBE;
long e__lb_xchoice[LB_XCHO];
long lb_root_field[LB_ROOT];
long e__field1[LB_DF1];
long lb_field2[LB_DF2];
long *e__ll;
long list_id;
long long_list_lenght;
char next_wnd_proc[LB_FWP][CHARS_MAX];
}lb_table;
Unless I am totally out of it, I don't think they are all 3 identical!
And three more nearly identical structs. Why, oh why?
Because again, these other 3 structures in my real code, are really:
=========================================#1
typedef struct tag_pc {
long gso_eeprom_address;
int gso_arr_address;
short pc_entry_mode;
long LK__F1;
long LK__F2;
long LK__F3;
int digits;
struct tag_pc_table *dc;
int dc_size;
int x_start_pos;
int y_start_pos;
int y_end_pos;
int char_mask_intrvl;
int msg_char_mask;
int sys__tot_itms_in_list;
int sys__list_item_idx;
long sys__curr_fwp_idx;
long sys__exodus;
} pc;
========================================#2
typedef struct tag_ddlb
{
long gso_eeprom_address;
int gso_arr_address;
long LK__F1;
long LK__F2;
long LK__F3;
long dc_size;
struct tag_ddlb_table *dc;
long sys_question_idx;
int sys__tot_itms_in_list;
int sys__list_item_idx;
int sys__ddlb_state_flag;
long sys__curr_fwp_idx;
long sys__exodus;
} ddlb;
========================================#3
typedef struct tag_lb
{
long gso_eeprom_address;
int gso_arr_address;
short menu_entry_mode;
long LK__F1;
long LK__F2;
long LK__F3;
struct tag_lb_table *dc;
long dc_size;
long *pmr;
long pmr_row;
long pmr_col;
int sys__tot_itms_in_list;
int sys__list_item_idx;
long sys__selected_pmr;
long sys__curr_fwp_idx;
long sys__exodus;
}lb;
===========================================
So, now what! Do we still feel that this would work:
void f1(void *x)
{
pc *p = x;
p->LK__F1 = 10;
p->dc[0].LKdct__F1 = 20;
}
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.
Look, if I do this:
======================================
int main()
{
void *x;
pc_table apc[] = { 101, 201...............}; // Innitialize all members here
ddlb_table addlb[] = { 102, 202.........}; // Innitialize all members here
lb_table alb[] = { 123, 200................}; // Innitialize all members here
x = malloc (sizeof (struct tag_lb));
x->dc = alb;
f1(x, 2);
=========================================
And for instance, if in f1() we have the only cast as so:
pc *p = x;
how is f1() ever going to get the value of Q_MBS which is in the
lb_table.... notice that Q_MBS does not exist in pc_table! So therefore
various casts have to exist in
f1() and based on what we pass in as the x parameter of F1() we have to
execute the correct cast... no?
I don't know, maybe its me! Theres something I don't understand.
Note that you have three calls to malloc(), but only one call to free().
You are leaking two blocks of memory.
I know, I know.
--
Best regards
Roberto
"Igor Tandetnik" wrote:
Robby <Robby@discussions.microsoft.com> wrote:
Bare in mind
What a disturbing image. Though I suppose "bear in mind" is not much
better. Now I'm going to have nightmares involving clean-shaved bears.
=================================
typedef struct tag_lb_table
{ long LKdct__F1;}lb_table;
typedef struct tag_ab_table
{ long LKdct__F1;}ab_table;
typedef struct tag_ab_table
{ long LKdct__F1;}cd_table;
These three structs are still identical. Why again are you having three
of them?
//============================
typedef struct tag_pc {
long LK__F1;
struct tag_pc_table *dc; // Pointer to an array of tag_pc_table
structs } pc;
typedef struct tag_ab {
long LK__F1;
struct tag_ab_table *dc; // Pointer to an array of tag_ab_table
structs } ab;
typedef struct tag_cd {
long LK__F1;
struct tag_cd_table *dc; // Pointer to an array of tag_cd_table
structs } cd;
And three more nearly identical structs. Why, oh why?
void f1(void *x, int type)
{
pc *p = x;
ab *q = x;
cd *r = x;
switch(type)
{
case 1: p->LK__F1 = 10; p->dc[0].LKdct__F1 = 20; break;
case 2: q->LK__F1 = 10; q->dc[0].LKdct__F1 = 20; break;
case 3: r->LK__F1 = 10; r->dc[0].LKdct__F1 = 20; break;
}
}
Just make it
void f1(void *x)
{
pc *p = x;
p->LK__F1 = 10;
p->dc[0].LKdct__F1 = 20;
}
int main()
{
void *x;
lb_table alb[] = { 100, 200};
ab_table aab[] = { 101, 201};
cd_table acd[] = { 102, 202};
x = malloc (sizeof (struct tag_pc));
x->dc = alb;
f1(x, 1);
x = malloc (sizeof (struct tag_ab));
x->dc = aab;
f1(x, 2);
x = malloc (sizeof (struct tag_cd));
x->dc = acd;
f1(x, 3);
free(x);
Note that you have three calls to malloc(), but only one call to free().
You are leaking two blocks of memory.
So how would I do a base structure for the 3 structs above (tag_pc,
tag_ab and tag_cd) given this scenario?
I say you get rid of three identical structures, and declare just one.
Here in f1(), I had to cast every possible type and put it through a
switch/case statement, which is what I am trying to get rid of. But
if I want to get rid of the casts
how would f1() know what type we are passing in so it accesses
the correct array of structs for the following line:
p->dc[0].LKdct__F1 = 20;
Why would it care? They all have the same layout.
--
With best wishes,
Igor Tandetnik
With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925