-3

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};
}
Leo
  • 3
  • 2
  • 1
    Compound literals were introduced in C99, your code seems correct, what is the exact error? _I think Codeblocks automatically uses pointer and modifies the code_. Codeblocks is an IDE not a compiler, and no, the compiler doesn't return any pointer, it returns a `Result` or it doesn't compile (depending on your version). – David Ranieri Nov 25 '20 at 11:52
  • Thanks for responding. The error i'm getting is error C4576: a parenthesized type followed by an initializer list is a non-standard explicit type conversion syntax on visual studio. – Leo Nov 25 '20 at 11:55
  • I would have bet 1000 dollars :) – David Ranieri Nov 25 '20 at 11:58
  • Visual Studio doesn't support the C language well. As for Codeblocks/mingw/gcc, they shipped it with a fairly old version of gcc for quite some time. Any version before 5.0 defaults to C90, even though they supported standard C too. – Lundin Nov 25 '20 at 12:01
  • 2
    Check this: [Compound literals in MSVC](https://stackoverflow.com/q/3869963/1606345) – David Ranieri Nov 25 '20 at 12:02
  • 1
    When answering requests for more information, edit the question to provide the information. Answering in comments in insufficient. – Eric Postpischil Nov 25 '20 at 12:05
  • Thank you, everyone for responding so kindly. Helped me a lot! – Leo Nov 25 '20 at 12:07
  • I didn't find any pointer related error, there is no use of pointers in your code – milevyo Nov 25 '20 at 15:08

1 Answers1

1

Your compiler does not support standard C. Change

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

to:

{
    Result temp = {sum, mean, var};
    return temp;
}
Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312