Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

need help from a c guru on use of structures

Status
Not open for further replies.

subra

Programmer
Aug 6, 2000
45
IN
I am a new C programmer. I use turbo c on a DOS operationg system. Please restrict your replies to Turbo C on DOS. Thanks.

My problem.

I have two databases which differ slightly but significantly. The databases are defined as struc which gives the field description. My program essentially performs the same operations on both the data bases. eg:

struc {
fields } datarec1;

struc {
fields } datarec2 ;

my_program1
{
datarec1.fieldname (some operation)
}

my_program2
{
datarec2.fieldname (some operation)
}

I have had to write two functions to do essentially the same thing. The only difference is that one accesses the struc datarec1 the other datarec2.

My question.

Is there some way in which I can use datarec1 and datarec2 in the same function. In other words can the same function be used to manipulate the two strucs which differ from each other and if yes, how does one do it?

Secondly, by doing so, will it result in significant drop in program size or execution speed?

Thank you.

Subra if you find this useful let me know at vksubra@icenet.net
 
ya you can do it.
i would have given you a sample had
your question been more clear


anyway if i understand you correctly
and if this is a PURE C prog

do like this,there may be better ways
but this might give you an idea

//name refers to the struct type
//datarec1 or datarec2
func(const char *name,void *sptr,your data)
{
((name *)sptr)->var=your data ;

}

try this and correct me if i am wrong or
i have misunderstood your question

bye





 
You say you use basically the same operations... Well... If the code itself in the 2 functions is very similar, you could use a MACRO definition and call that macro instead of functions... but, this depends strongly on the fields of your structs, since a macro barely replaces text in the C file...
Do the 2 structures have a common part?
There are a lot of possible solutions to your problem, but the best one depends on the exact structure of the structures.

You might say something like:
Code:
struct {
   field1;
   field2;
  ...
} CommonDataBasePart;

struct {
  field11;
  field12;
  ...
} DataBase1Private;

struct {
   field21;
   field22; 
  ...
} DataBase2Private;

struct {
   CommonDatabasePart* common;
   DataBase1Private* db1;
   DataBase2Private* db2;
} DBMS :-);

You could use one function like
Code:
void function(DBMS * database)
{
   ... perform CommonDataBase Functions here...
   ...
   if (database->db1)
      perform DataBase1Private functions here
   if (database->db2)
      perform DataBase2Private functions here
}
and call this functions with the appropriate members set to non-Null pointers.

There are also other ways to simulate some sort of C++ polymorphism you want to use here, that is using one function on several datatypes. This can be a long reply, so please be more specific.

Anyway, using one function instead of two functions surely reduces the program size, but that depends on the size of the function.
And, to make the program as fast as possible, use the less number of functions you can, possibly try use macros at a full scale, but, then you will find that AWFULLY hard to debug...

Hope this helps [red]Nosferatu[/red]
We are what we eat...
There's no such thing as free meal...
 
i shall try to clarify further:

the actual structures are given below

typedef struct /* This is the ioa record definition */
{
char scheduleno[3]; /* schedule no */
char postdate[11]; /* postdate */
char respdate[11]; /* responding date */
char brcode[5]; /* branch code */
char amount[16]; /* amount */
} IOARECORD;

typedef struct /* This is the dft record definition */
{
char scheduleno[3]; /* schedule no */
char postdate[11]; /* postdate */
char respdate[11]; /* responding date */
char brcode[5]; /* branch code */
char refno[11]; /* reference no */
char amount[16]; /* amount */
} DFTRECORD;


you can see that the structures are the same in all respects except that the dft record has an extra field, refno.

please assume that the necessary data files and index files have been opened and setup correctly. i have a function which accepts a key value and then indexes the data file on that key like this:

indexfunction_ior(key_str)
{
// create the index using key_str
index(ioarecord.postdate) // some indexing code
/* for each record read the function has to access
ioarecord.postdate, the key field. hence the need
to access with structure.fieldname */
}
indexfunction_dft(key_str)
{
// create the index using key_str
index(dftrecord.postdate) // some indexing code
/* for each record read the function has to access
ioarecord.postdate, the key field. hence the need
to access with structure.fieldname */

}

the function now creates two index files on the key postdate. the functions are the same except that the key value passed to it differ (though the key value is essentially the same, it is read in from a different structure.) is there some way in which i can have a general way in which can pass the key values. please let me know if you need further clarifications.

thanks for responding promptly.

subra if you find this useful let me know at vksubra@icenet.net
 
subra ,
that was detailed
what i understand is
1. the struct data members
2.the similarity in them

what i need clarification

1.how you want to store

give a sample of data as it would be
stored in the .txt file for both

2.if you are using binary which becomes
handy when dealing with structures
then please clarify


3 .your index functions and what they
want to accomplish

bye
 
Well then, i quess that my previous answer should help you... [red]Nosferatu[/red]
We are what we eat...
There's no such thing as free meal...
 
I see the C style object oriented programming become a frequently asked question. It'll just appear in FAQ aera. Ion Filipski
1c.bmp


filipski@excite.com
 
can i return multiple pieces of float type data or pointers to float type data as the return of a function (eg):
float calculate(float a, float b){
float pass_this[3];
pass_this[0] = CalcInstall(a);
pass_this[1] = CalcSubtotal(pass_this[0], b);
pass_this[2] = CalcTotal(pass_this[1]);
return pass_this;
}
more_data[3];
more_data = calculate(area(data[0], data[1]), data[2]);
then store the return in a predefined float array of the same size?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top