
Comparing Structure Elements
Help Please ...
The following code fragment shows my current way of comparing two
structures to find elements which have the same text identifier
(tag_name). What I am doing is creating a structure which holds a
name and a value, sometime later, another struture is updated with
the same names and new data (.value).
I don't think that this is a very efficient way of doing it, does
anyone have any better solutions that they would like to share ? As
you can probably tell, I have no real experience with
sorting/searching records etc, so any help would be appreciated
regards
Dave
------------------------------------
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define RECORDS 100 /* Could be more though */
#define MAX_FIELD_SIZE 10
#define MAX_LINE_LENGTH 40
typedef struct {
char tag_name[MAX_FIELD_SIZE+1];
long value;
}pml_data;
pml_data pml_current[RECORDS]; /* Array to hold new PML totals */
pml_data pml_history[RECORDS]; /* Array to hold old PML totals */
/* Code to populate the structures is omitted to keep this brief */
int main()
{
int i,j; /* loop counters */
long difference = 0;
for (i = 0; i < PML_RECORDS; i++)
{
for (j = 0; j < PML_RECORDS; j++)
{
if ((strcmp(pml_current[i].tag_name,
pml_history[j].tag_name)) == 0)
{
difference = pml_current[i].value
- pml_history[j].value;
break;
}
else;
} /* j */
} /* i */
Quote:
}
}