Hello quzah,
Thankyou for your response. One thing though, in your sample, if t = 1, I will do something... with... struct1, then another time if t = 2, I will do something else ...with struct2. So like this:
Code:
#include <stdio.h>
typedef struct tag1{
int a;
long h;
}t1;
typedef struct tag2{
int a;
int *y;
}t2;
void f1(void *i, int s)
{
if(s == 1)
{
t1 *X = (t1*)i;
X->a = 9;
}
else if(s==2)
{
t2 *Y = (t2*)i;
Y->a = 11;
}
}
int main()
{
t1 x;
t2 y;
f1(&x,1);
f1(&y,2);
}
In my project, this so called "something" that we do for struct1 is also done identically for struct2 and can be 30 or 40 lines long! Also, to remind ourselves that t can be 1 to perhaps 50 or 100.... therefore, f1() will be very long with alot of if's/else statements containing the the same 50 or 100 lines. Here let me show a sample function in my real code:
Code:
void DDLB_retrieve_from_eeprom(
ddlb *obj_ddlb, enum eSPI_MEMBANK Eep_Bank_Num,
unsigned short eeprom_gsoa)
{
unsigned char j, k, s, totLists;
unsigned char eeprom_buff[4096];
unsigned char eep_Val1 = 0, eep_Val2 = 0;
totLists = obj_ddlb->dc_size;
ULC_FLASH_SPI_high_speed_read(eeprom_buff, Eep_Bank_Num, 0, 0, 4096);
eep_Val1 = eeprom_buff[eeprom_gsoa];
eep_Val2 = eeprom_buff[++eeprom_gsoa];
obj_ddlb->gso_address = API_combine_chars_to_short(eep_Val1, eep_Val2);
for(j=0; j<totLists; j++)
{
eep_Val1 = eeprom_buff[++eeprom_gsoa];
eep_Val2 = eeprom_buff[++eeprom_gsoa];
obj_ddlb->F__choice[j] = API_combine_chars_to_short(eep_Val1, eep_Val2);
eep_Val1 = eeprom_buff[++eeprom_gsoa];
eep_Val2 = eeprom_buff[++eeprom_gsoa];
obj_ddlb->F__list_lenght[j] = API_combine_chars_to_short(eep_Val1, eep_Val2);
}
for(j=0; j<totLists; j++)
{
k = (gsoDdlbx->F__list_lenght[j]);
obj_ddlb->E__ll[j] = malloc(k * sizeof(unsigned short)); // freed elsewhere!
for(s=0; s<k; s++)
{
eep_Val1 = eeprom_buff[++eeprom_gsoa];
eep_Val2 = eeprom_buff[++eeprom_gsoa];
(obj_ddlb->E__ll[j])[s] = API_combine_chars_to_short(eep_Val1, eep_Val2);
}
}
}
So as you can see, I would like to have the code above written once in my function while being able to cast the first argument to the appropriate type. Right now I always have to pass in a pointer to a ddlb struct. Hence, I have this function written 5 times in my code.... to acomodate 4 other types. Its very sad! In the future I can end up with 50 types as opposed to the 5 types I have now. If I start embeding the above code in 50 "if/else if" statements.............. I don't know about you, but I just don't like it! Theres got to be a better way!
laserlight suggested ... function-style macro instead of a function. I wouldn't know exactly how to do this. If someone can direct me to some samples on the Internet... that would be very appreciated. I looked for this myself, but unfortunately I did not find anything that suited my issue.
Standing by... please do get back.... all help and suggestions are welcome and appreciated! Thanks