Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 637921
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T20:40:35+00:00 2026-05-13T20:40:35+00:00

I know, that if i want to compare two structs than i have to

  • 0

I know, that if i want to compare two structs than i have to write it for myself, because there isn’t any function for this, but i can’t figure out how should i do that. I have three structs : primary, secondarystruct, and difference(this should contain the different items). All three has the following members : char * filename, char * size, int size.

All i need are those items which aren’t in the secondarystruct , or if they are then i need them only if their size is bigger then the secondarystruct’s size. Hope you understand what i want. My english isn’t the best, sorry for this.

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, it seems, i gave you not enough information.
So: both structures are containing file list, from different drives, like “C:\” and “D:\”. This is the reason why i can’t use just simple strcmp, because the first letter will always differ. That’s why i have to “cut them off” and then compare. This program should work like this: It retrieves the file list from c:\ and then retrieves the filelist from d:\ and then compares them. If on file which is on c:\ doesn’t exists on d:\ then it should be copied there, if on d:\ there is a file which doesn’t exists on c:\ then it should be ignored(i don’t wan’t to do with it anything). If a file which is found in c:\ and d:\ as well, then i wan’t to copy it only then if the file from c:\ has a bigger size than a file which is on d:\

Hope you understand now what i want.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T20:40:35+00:00Added an answer on May 13, 2026 at 8:40 pm

    The most probable cause of your “freeze” is the strlen() call, which is likely caused by some memory problem (ie. that its argument isn’t a pointer to string terminated by zero). There may be multiple causes of this:

    • you may have overwritten some of your memory by a buffer overflow (of tmp or buf).
    • I assume you’re using x as an indicator you’ve gone to the end, but you use secondarystruct[j] after that. Moreover, if secondarypcs has the same meaning as primarypcs, that is, the count of elements of an array, you’re using secondarystruct[secondarypcs], but that’s out-of-bounds.

    Some other tips:

    • if the first file in secondarypcs is missing in primarypcs, your code will put everything to diff, no matter what.
    • comparing the strings regardless of the first letter can be done like this:

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

    I’d suggest 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's it
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In PHP, I want to compare two relative URLs for equality. The catch: URLs
I need a simple and fast way to compare two images for similarity. I.e.
More specifically I want an interface to compare objects which can be only compared
Using Ruby 1.9.2 Problem Compare the content, not the results, of two procs. I
I know a java treeset can't have identical elements and so I have to
I lack industry experience with the language. But in my spare time I have
I have the following base abstract class defined as: public abstract class BaseObject<T> :
I've got one class, that I sort it already by one attribute. Now I
A colleague came to me with a problem that I managed to answer but
I'm trying to make a zoom system for a C#/XNA game I'm working on.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.