在c中使用struct

Arm*_*maa 1 c loops

#include <stdio.h>   
struct Bar{
    int max;
    int N;
    int k[4];
    float g[4];
};


typedef struct Bar myStruct;



myStruct entr(){
    myStruct result;
    int i;

    printf("max\n");
    scanf("%d", &result.max);

    printf("N = \n");
    scanf("%d", &result.N);

    printf("\nEnter k = ");
    for(i=1; i<=4; i++) scanf("%d", &result.k[i]);

    printf("\ng = ");
    for(i=1; i<=4; i++) scanf("%f" , &result.g[i]);

    return result;
}


void main() {       
    myStruct entrs=entr();
}
Run Code Online (Sandbox Code Playgroud)

我在linux中运行此代码(使用gcc编译),每次出现以下错误

" *堆栈粉碎检测到*:./a.out终止中止"

我该如何解决这个错误?**

Sou*_*osh 7

问题在于边界(溢出).

在你的情况下

  for(i=1; i<=4; i++)
Run Code Online (Sandbox Code Playgroud)

应该

  for(i=0; i<4; i++)
Run Code Online (Sandbox Code Playgroud)

因为C数组使用基于0的索引.否则,使用您的代码,您就是

也就是说,void main()应该int main(void)让托管环境符合标准.

  • 这是正确的回应.没有什么可以补充的,干得好. (2认同)