How to compare two structures in c?

I know that if I want to compare two structures, then I have to write this for myself, because there is no function for this, but I cannot figure out how to do this. I have three structures: primary, secondary structure and difference (this should contain different elements). All three have the following members: char * filename, char * size, int size.

All I need are those elements that are not in the secondary structure, or if I need them then, only if their size is larger than the size of the secondary structure. I hope you understand what I want. My English is not the best, sorry for that.

Here is what I tried:

j = 0; x = 0; for ( i = 0; i < primarypcs; ) { memset( tmp, 0, sizeof( tmp ) ); l = 1; for ( k = 0; k < strlen( primary[i].filename );k++ ) { tmp[k] = primary[i].filename[l]; l++; } tmp[k]='\0'; memset( buf, 0, sizeof( buf ) ); l = 1; for ( k = 0; k < strlen( secondarystruct[j].filename ); k++ ) //<-- here is where my program freezes { buf[k] = secondarystruct[j].filename[l]; l++; } buf[k]='\0'; if ( ( stricmp( tmp, buf ) == 0 ) && ( x == 0 ) ) { if ( primary[i].intsize > secondarystruct[j].intsize ) { difference[diff].filename = strdup( primary[i].filename ); difference[diff].size = strdup( primary[i].size ); difference[diff].intsize = -1; diff++; i++; if ( j == secondarypcs ) x = 1; else j++; } else if ( x == 0 ) { i++; if ( j == secondarypcs ) x = 1; else j++; } } else { difference[diff].filename = strdup( primary[i].filename ); difference[diff].size = strdup( primary[i].size ); difference[diff].intsize = -1; diff++; i++; } } 

Please tell me what I'm doing wrong!

Thanks kampi

Update:

Sorry, I seem to have given you not enough information. So: both structures contain a list of files from different drives, such as "C: \" and "D: \". It is for this reason that I cannot use just plain strcmp, because the first letter will always be different. That is why I have to "disable them" and then compare. This program should work as follows: it extracts a list of files from c: \, and then extracts a list of files from d: \, and then compares them. If the file that is on c: \ does not exist on d: \, then it should be copied there, if on d: \ there is a file that does not exist on c: \, then it should be ignored (I should not do nothing about it). If the file that is in c: \ and d: \, then I will not copy it only if the file from c: \ is larger than the file that is in d: \

I hope you now understand what I want.

+4
source share
5 answers

The most likely reason for your “freezing” is to call strlen() , which is probably caused by some memory problem (i.e. that its argument is not a pointer to a line terminated by zero). There may be several reasons:

  • You may have overwritten part of your memory with a buffer overflow ( tmp or buf ).
  • I assume that you use x as an indicator, which you went through to the end, but after that you use secondarystruct[j] . Moreover, if secondarypcs has the same meaning as primarypcs , that is, an array element counter, you use secondarystruct[secondarypcs] , but that’s out of bounds.

Some other tips:

  • If the first file in secondarypcs missing in primarypcs , your code will put everything in diff, no matter what.
  • string comparison regardless of the first letter can be performed as follows:

    (* str1 & * str2? strcmp (str1 + 1, str2 + 1): -1)

I would suggest a code like this:

 void add_to_difference(struct diff_file* f); ... // assuming primarystruct and secondarystruct are arrays of diff_file sorted by filename j=0; for(i=0; i<primarypcs; i++) { // find a passibly matching secondary file while(j<secondarypcs && strcmp(primarystruct[i].filename+1, secondarystruct[j].filename+1)<0) j++; // not found... add all overflow items to diff if(j>=secondarypcs) { for(; i<primarypcs; i++) add_to_diff(primarystruct+i); break; } // do the comparison if(strcmp(primarystruct[i].filename+1, secondarystruct[j].filename+1)>0 || primarystruct[i].intsize>secondarystruct[j].intsize) add_to_diff(primarystruct+i); // that it } 
+2
source

If you have the same structural variables. Access each member of the structural variables, then perform the appropriate comparison. you need to compare values ​​based on their data types.

+1
source

you can compare structures using memcmp () function. If you specify structures and lengths in memcmp, it will compare and return the results in an integer like strcmp.

But this is better than the strcmp () function. It directly relates to memory. therefore, it can accurately produce results.

+1
source

You should compare field structs by field. Use strcmp for char * strings and comparison operators for integers. Usually you perform a separate function for comparison, and the function will return 0 if the structures are the same, a negative value if the first value is “less” and a positive value if the first is more.

0
source

This is too much code if you just want to execute what you say.

Just use strcmp () to compare file names and sizes and ">" for size. Then, based on the results of the comparison, you can copy the member members into the difference structure using strdup () or a simple "=" for integers.

  if (! strcmp (first.filename, second.filename) {
     ... what to do, when they are different ...
 }
 if (! strcmp (first.size, second.size) {
     ... what to do, when they are different ...
 }
 if (first.intsize! = second.intsize) {
     ..etc ...
 }
0
source

Source: https://habr.com/ru/post/1302958/


All Articles