主题:写的一个练习程序,Segmentation fault
[code=c]
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
*求方差,公式为[(x1-y)^2+(x2-y)+……]/n,其中xi为所求方差的数字,y为平均值,n为
*所求方差的数字的个数
*要求是输入整形数,返回整形数
*/
#include <stdio.h>
#include <math.h>
//int function(int *calculate[],int arrayLen);
int main(void)
{
int calculate[3]={1,2,3};
int arrayLen=3;
printf("%d\n",function(calculate[3],3));
}
int function(int calculate[],int arrayLen)
{
int i;
float sum,average,square,square_average;
for(i=0;i<arrayLen;i++)
sum+=calculate[i];
average=sum/arrayLen;
for(i=0;i<arrayLen;i++)
square_average+=((float)calculate[i]-average)
*((float)calculate[i]-average);
return (int)square_average/arrayLen;
}
[/code]
在不注释main函数前的function函数声明时,会出现问题:
[quote]
[zhuqx_hp@node1 test]$ gcc huawei.c -o huawei
huawei.c: In function `main':
huawei.c:16: warning: passing arg 1 of `function' makes pointer from integer without a cast
huawei.c: At top level:
huawei.c:20: error: conflicting types for 'function'
huawei.c:9: error: previous declaration of 'function' was here
huawei.c:20: error: conflicting types for 'function'
huawei.c:9: error: previous declaration of 'function' was here
huawei.c:32:2: warning: no newline at end of file
[zhuqx_hp@node1 test]$
[/quote]
注释掉function函数的声明之后就好了。
这是怎么回事呢?请教大家给予解释。
但是编译的时候会提示段错误
[quote]
[zhuqx_hp@node1 test]$ gcc huawei.c -o huawei
huawei.c:32:2: warning: no newline at end of file
[zhuqx_hp@node1 test]$ gcc huawei.c -o huawei
[zhuqx_hp@node1 test]$ ./huawei
Segmentation fault
[/quote]
请教各位,这是怎么回事,应该怎么解决呢?
谢谢各位。
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
*求方差,公式为[(x1-y)^2+(x2-y)+……]/n,其中xi为所求方差的数字,y为平均值,n为
*所求方差的数字的个数
*要求是输入整形数,返回整形数
*/
#include <stdio.h>
#include <math.h>
//int function(int *calculate[],int arrayLen);
int main(void)
{
int calculate[3]={1,2,3};
int arrayLen=3;
printf("%d\n",function(calculate[3],3));
}
int function(int calculate[],int arrayLen)
{
int i;
float sum,average,square,square_average;
for(i=0;i<arrayLen;i++)
sum+=calculate[i];
average=sum/arrayLen;
for(i=0;i<arrayLen;i++)
square_average+=((float)calculate[i]-average)
*((float)calculate[i]-average);
return (int)square_average/arrayLen;
}
[/code]
在不注释main函数前的function函数声明时,会出现问题:
[quote]
[zhuqx_hp@node1 test]$ gcc huawei.c -o huawei
huawei.c: In function `main':
huawei.c:16: warning: passing arg 1 of `function' makes pointer from integer without a cast
huawei.c: At top level:
huawei.c:20: error: conflicting types for 'function'
huawei.c:9: error: previous declaration of 'function' was here
huawei.c:20: error: conflicting types for 'function'
huawei.c:9: error: previous declaration of 'function' was here
huawei.c:32:2: warning: no newline at end of file
[zhuqx_hp@node1 test]$
[/quote]
注释掉function函数的声明之后就好了。
这是怎么回事呢?请教大家给予解释。
但是编译的时候会提示段错误
[quote]
[zhuqx_hp@node1 test]$ gcc huawei.c -o huawei
huawei.c:32:2: warning: no newline at end of file
[zhuqx_hp@node1 test]$ gcc huawei.c -o huawei
[zhuqx_hp@node1 test]$ ./huawei
Segmentation fault
[/quote]
请教各位,这是怎么回事,应该怎么解决呢?
谢谢各位。