TIP: Click on subject to list as thread! ANSI
echo: c_plusplus
to: PAUL DRUGGITT
from: CLIFF RHODES
date: 1997-05-24 17:10:00
subject: passing structures to fu

--> Paul Druggitt wrote to Cliff Rhodes <--
 
PD> I have tried to pass a void pointer to my function, but cannot
PD> access  individual members of the structure to perform
PD> comparisons.
 
CR> Just pass the member. Or am I missing something in your
CR> question?
 
PD>     1) Pass ANY structure to a function, stating which member to
PD>     do comparisons on & which file to read/write. eg.
PD>        insert_record_in date_order(&BIGGER_DATA, BIGGER_DATA.date,
PD>        insert_record_in date_order(&DATA, DATA.date, fp1);
PD>     2) Read a record from a file of the same structure as that
PD>     passed to    the function.
PD>     3) Compare the member stated in the call to the function with
PD>     the    equivalent member in the record just read & change
PD>        values as required.
 
Ok, you'll need more information passed to the function. Here is a C
approach that might give you some ideas. See my comment below about C 
versus C++.
#include  
#include 
 
int membercmp(void *struc, size_t ssize, void *data, size_t dsize,
              FILE *fp)
{
   /* Given a pointer to some structure struc, and the size of
    * that type of structure, ssize, compare the member pointed
    * to by data of size dsize in struc and the next record
    * read from file fp.
    * Returns 0 if the members are equal, 1 if not.
    * !!!!!NOTE: Assumes all files are opened in binary mode!!!!!
    */
 
   int i, ret = 0;
 
   /* Get the offset of the data member in struc... */
   size_t doffset = (char *) struc - (char *) data;
 
   char *s = malloc(ssize);   /* Get space to save record */
 
   fread(s, ssize, 1, fp);    /* Read the record */
 
   for(i = 0; i < dsize; i++)
     if(s[doffset + i] != ((char *)data)[i])
        ret = 1;
 
   free(s);    /* Free the allocated space */
 
   return ret;
}
 
call as
 
membercmp(&DATA, sizeof(DATA), &DATA.date, sizeof(DATA.date),
          fp1);
 
You could even create a macro
#define Mcmp(x, y, f)  membercmp(&x, sizeof(x), &x.y, sizeof(x.y), f);
and call as
Mcmp(DATA, date, fp1);
PD>     4) Rewrite the changed records at their original positions
PD>        & new record at the end of the file
 
You should be able to easily change the above function to do this.
 
PD> I hope you now understand. It seems as clear as mud to me.
 
The way you have structured your problem is not good C++. Do you
intend to do this in C or C++?
 
If you want to use C++ you should consider inheritance and file
streams. By using inheritance, you can overload member functions so
that the compiler will automatically call the correct function
depending on the record you are using. You can overload the IO stream
operators to handle file operations correctly.
If you want to do it in C, you ought to switch to the C echo. :-)
 
Cliff Rhodes
cliff.rhodes@juge.com
 
X CMPQwk 1.42 1692 X"Nowadays most men lead lives of noisy desperation
--- Maximus/2 3.01
---------------
* Origin: COMM Port OS/2 juge.com 204.89.247.1 (281) 980-9671 (1:106/2000)

SOURCE: echomail via exec-pc

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.