结构体类型数组int成员变量求和函数

struct stru1
{
 int a;
 float b;
 int c;
 int d;
};

struct stru2
{
 int a;
 char b[100];
 int c;
 stru1 d;
};


struct stru1 s1[100];
struct stru2 s2[100];

...

int sum1 = supersumofint(&s1[0].c, 100, sizeof(stru1));
int sum2 = supersumofint(&s2[0].d.d, 100, sizeof(stru2));


int supersumofint(const int *iptr, int n, int strulen)
{
 int i;
 int sum;

 if (n & 1)
  {
   sum = *iptr;
   ((const char *)iptr) += strulen;
   i = 1;
  }
 else
  {
   sum = 0;
   i = 0;
  }

 for ( ; i < n; i += 2)
  {
   sum += *iptr;
   ((const char *)iptr) += strulen;
   sum += *iptr;
   ((const char *)iptr) += strulen;
  }

 return sum;
}

                         
               

你可能感兴趣的:(结构体类型数组int成员变量求和函数)