-
Re-use in C?
Is it possible to reuse the functions in C for different data types in run time?
For example: I have functions to process link list: T represent any data types:
Code:
typedef struct {
char data[10];
void* p_next;
int index;
} *pDateStruct;
typedef struct {
char data[2];
void* p_next;
int index;
} *pRecipeStruct;
T NewDateStruct(void)
{
T Newrptlst = NULL; /* Pointer on the new sructure */
/* create the new structure */
Newrptlst = (T) malloc(sizeof *Newrptlst);
if (!Newrptlst)
{
Newrptlst = NULL;
return Newrptlst;
}
/* Initialize the new structure */
memset (Newrptlst, 0, sizeof *Newrptlst);
Newrptlst->p_next = NULL;
Newrptlst->index = -1;
return Newrptlst;
}
T AddDateStruct(T *List)
{
T CurRptlst= NULL; /* Pointer on the current record */
T NewRptlst= NULL; /* Pointer on the next record */
T LatRptlst= NULL; /* Pointer on the last record */
if (*List == NULL) {
*List= NewDateStruct();
return *List;
} else {
CurRptlst= *List;
/* search the last record into the list */
while (CurRptlst != NULL && CurRptlst->index >= 0) {
LatRptlst = CurRptlst;
CurRptlst= (T) CurRptlst->p_next;
}
/* if the last struct is not NULL and its index is < 0, means it is created before and we can re-use it, so no need to allocate new memory */
if(CurRptlst != NULL && CurRptlst->index < 0)
{
return CurRptlst;
}
NewRptlst= NewDateStruct();
LatRptlst->p_next=NewRptlst;
return NewRptlst;
}
}
int DelDateStruct(T *List)
{
int ResCode = 0; /* Internal step result code */
T CurList = NULL; /* Pointer on the first record to be deleted*/
T NextList = NULL; /* Pointer on the next record */
CurList = *List;
while (CurList != NULL) {
NextList = (T) CurList->p_next;
free(CurList);
CurList= NextList;
}
if (ResCode == 0)
*List = NULL;
return ResCode;
}
To make it work or compile successfully, I have to add
Code:
#define T pDateStruct
or
Code:
typedef pDateStruct T;
Now the issue comes, I want to reuse these functions on pRecipeStruct also, is that possible in C?
-
Re: Re-use in C?
First, let's just stipulate you really mean C, and not C like code in a C++ compiler.
Since you don't have the ability to overload functions, and even in C++ you can't create overloads that differ only in the return type, you are close to a "'tween a rock and a hard place" situation.
You've just about outlined an exercise that demonstrates why most people agree C++ is superior to C (sorry, I don't mean to knock C - I wrote in C from late seventies to '90/91).
Obviously you can't define T multiple times, but you could use a macro to generate a small function that's name-mangled, essentially - and take that through to it's logical conclusion - somewhat mimicking the name mangling that gives rise to object types in C++ ( originally, C++ generated C code - like a text processor).
-
Re: Re-use in C?
sandodo,
I believe that you can obtain some level of generic coding in this matter in the C language by designing the lists with exclusive use of the void*. You only need to know the size of the object when creating one of them with malloc. The calls to malloc can be implemented using macro technologies as mentioned by JVene. When accessing the objects, the void pointers have to be de-referenced. The casting involved in the C language can be somewhat non-intuitive and bulky.
I never did the linked list in C using void*, however years ago i did some other kinds of stacks and object buffers using void* in C.
Sincerely, Chris.