简体   繁体   English

嗨,我在使用指针时遇到问题

[英]Hi, I have trouble using pointers

This C code works fine in Codeblocks but not in other environments.此 C 代码在代码块中工作正常,但在其他环境中却不行。 I think Codeblocks automatically uses pointer and modifies the code我认为 Codeblocks 会自动使用指针并修改代码

return (Result){sum, mean, var};返回(结果){sum,mean,var}; << This part has an error in visual studio. << 这部分在visual studio中有错误。 error C4576: a parenthesized type followed by an initializer list is a non-standard explicit type conversion syntax错误 C4576:带括号的类型后跟初始化列表是非标准的显式类型转换语法

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.您的编译器不支持标准 C。 Change改变

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

to:至:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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