D
david.chanters
Hello all,
I am curious to know some best practise techniques when traversing/
accessing struct members. At the moment I have the following
declarations:
typedef struct PayInfo
{
char *name;
char *address;
int employeeNum;
int internalId;
PayInfo *pinfo;
} PayInfo;
// Initialise an instance of PayInfo here...
Which acts as a linked list, that's fine. I then, have several
functions which look like this:
char *GetEmpname(int number)
{
PayInfo pi;
pi = MyInitializedPayInfo->next;
while(pi != NULL && pi->employeeNum != number)
pi = pi->next;
if (pi != NULL)
return pi->name;
return NULL;
}
That's just one example, I also have:
char *GetAddress(int number);
int GetInternalId(int number);
Which do the exact same thing as GetEmpname() but returns different
data.
My thinking is that this is inefficient, especially if/when the struct
has more members added to it. My thoughts are to have a enum which is
passed around, so I could say something like:
GetGenericMember(PayInfo *pi, enum flags)
Where this enum would list which attribute I want returned. Is this a
good idea? Are there some other way of doing this I am not thinking
about, or am I just missing the point, and instead should just have
one function per data member I want returned?
Suggestions welcome, and thanks in advance.
David.
I am curious to know some best practise techniques when traversing/
accessing struct members. At the moment I have the following
declarations:
typedef struct PayInfo
{
char *name;
char *address;
int employeeNum;
int internalId;
PayInfo *pinfo;
} PayInfo;
// Initialise an instance of PayInfo here...
Which acts as a linked list, that's fine. I then, have several
functions which look like this:
char *GetEmpname(int number)
{
PayInfo pi;
pi = MyInitializedPayInfo->next;
while(pi != NULL && pi->employeeNum != number)
pi = pi->next;
if (pi != NULL)
return pi->name;
return NULL;
}
That's just one example, I also have:
char *GetAddress(int number);
int GetInternalId(int number);
Which do the exact same thing as GetEmpname() but returns different
data.
My thinking is that this is inefficient, especially if/when the struct
has more members added to it. My thoughts are to have a enum which is
passed around, so I could say something like:
GetGenericMember(PayInfo *pi, enum flags)
Where this enum would list which attribute I want returned. Is this a
good idea? Are there some other way of doing this I am not thinking
about, or am I just missing the point, and instead should just have
one function per data member I want returned?
Suggestions welcome, and thanks in advance.
David.