Forum

Number Of Page View:100,000+/Months
LOGIN |New User

Reply

How can we check whether the contents of two structure variables are same or not?
Here below I given a fully tested program which solve your problem.

In this i given two structures named str1 and str2.
Program return 1 when they have been matched other wise return 0. You can run this program in VC++.

#include <stdio.h>
#include <conio.h>
#include <string.h>

compare(i,j,l)
char *i, *j;
int l;
{
   int n;
   for (n=0; n<l; n++)
   if (*(i+n) != *(j+n) )
   break;
   if (n==l)
   return(1);
   else
   return(0);
}
struct str {
  int n;
  char s[5];
  double d;
};

main()
{
   int strs, compare();
   struct str str1, str2;
   int a;
   double d;
   char s[5];
   printf("Enter str1 values (int, char 4,   double)-->");
   scanf("%d %s %lf", &n,s,&d );
   str1.a=n; 
   strcpy(str1.s,s);
   str1.d=d;
   printf("Enter str2 values (int, char 4, double)-->");
   scanf("%d %s %lf", &n,s,&d );
   str2.a=n;
   strcpy(str2.s,s);
   str2.d=d;
   strs=compare ((char *)&str1, (char *)&str2, sizeof(struct str));
   printf("strs = %d
", strs);
}