[英]return dynamic array from function to main in C
returning to arrays to main, compiles but at run time gives lap2: malloc.c:2451: sYSMALLOc message. 返回到main的数组,进行编译但在运行时给出lap2:malloc.c:2451:sYSMALLOc消息。 I think the error is to do with minutes() and seconds().Help would be much appreciated.
我认为错误是与minutes()和seconds()有关的。帮助将不胜感激。
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#define N 25
int *minutes(float);
float *seconds(float);
int main ()
{
int *m;
float speed,min,sec,*s;
printf("Enter minute and seconds\n");
scanf("%f %f:\n", &min, &sec);
speed = ((min * 60) + sec) / N;
m = minutes(speed);
s = seconds(speed);
printf("%p:%p\n",m,s);
free(m);
free(s);
return 0;
}
int *minutes(float l)
{
int i,*h;
h = calloc(N,sizeof(int));
if(!h){
printf("memory failure\n");
exit(1);
}
for(i=1;i<=N;i++)
h[i] = ((int) l * i) / 60;
return h;
}
float *seconds(float m)
{
int i;
float j,*k;
k = calloc(N,sizeof(float));
if(!k){
printf("memory failure\n");
exit(2);
}
for(i=1;i<=N;i++)
j = i * m;
k[i] = fmod(j,60.0);
return k;
}
In your seconds(float)
function this 在你的
seconds(float)
功能
for(i=1;i<=N;i++)
j = i * m;
k[i] = fmod(j,60.0);
Should probably be 应该是
for (i = 1; i <= N; i++) {
j = i * m;
k[i] = fmod(j, 60.0);
}
At least, it seems to function when I use that... eg 至少,当我使用它时它似乎起作用了……例如
Enter minute and seconds
10 5 :
0xe1b010:0xe1b080
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.