简体   繁体   中英

Hi, I have trouble using pointers

This C code works fine in Codeblocks but not in other environments. I think Codeblocks automatically uses pointer and modifies the code

return (Result){sum, mean, var}; << This part has an error in visual studio. error C4576: a parenthesized type followed by an initializer list is a non-standard explicit type conversion syntax

Would there be any suggestion that can fix the code to use pointers? Thank you for reading my question!

#include<stdio.h>
#define SIZE 6
typedef struct Result {
   float sum, mean, variance;
}Result;

Result Statistics(int a[]);

int main()
{
   int array[SIZE] = { 1,2,3,4,5,6 };
   Result result;
   result = Statistics(array);
   printf("Sum=%.2f\n", result.sum);
   printf("Mean=%.2f\n", result.mean);
   printf("Variance=%.2f\n", result.variance);
   return 0;
}


Result Statistics(int a[])
{

   float sum, mean, var, sum2, diff;
   int i;
   for (sum = 0, i = 0; i < SIZE; i++) {
      sum += (float)a[i];

   }
   mean = sum / SIZE;
   for (sum2 = 0, i = 0; i < SIZE; i++) {
      diff = (float)a[i] - mean;
      sum2 += diff * diff;
   }
   var = sum2 / SIZE;
   return (Result){sum, mean, var};
}

Your compiler does not support standard C. Change

return (Result){sum, mean, var};

to:

{
    Result temp = {sum, mean, var};
    return temp;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM