我的问题是,当我将函数“sense”收集的指针数组“q”传递给其他函数“move”时,“q”中的值会随机变化。任何回应将不胜感激。这是我的代码:
Main()
int main(int argc,char *argv[])
{
int i,j,k;
float *p=(float *)malloc(sizeof(float));
float *q=(float *)malloc(sizeof(float));
j=0;
k=0;
for(i=0;i<NCELLS;i++)
p[i]=1.0/NCELLS;
q=sense(p,j); //Gives write values in q
j++;
q=move(q,motion[k]); // when passed to "move" values of q randomly changes
k++;
printf("\n");
for(i=0;i<NCELLS;i++)
printf("%f\t", q[i]);
printf("\n");
return 0;
}
functions:
float* move(float *p,int U)
{
int i;
float temp;
float *next=(float *)malloc(sizeof(float));
printf("\n");
for(i=0;i<NCELLS;i++)
printf("%f\t", p[i]); //Here I am checking for change in values
for(i=0;i<NCELLS;i++)
{
temp=pGoal*p[mod(i-U,NCELLS)];
temp=temp+pOvershoot*p[mod(i-U+1,NCELLS)];
temp=temp+pUndershoot*p[mod(i-U-1,NCELLS)];
next[i]=temp;
}
return(next);
}
float* sense(float *p,int j)
{
int i;
float *q, sum;
q=(float *)malloc(sizeof(float));
sum=0;
for(i=0;i<NCELLS;i++)
{
if(strcmp(World[i],Sense[j])==0)
q[i]=pHit*p[i];
else
q[i]=pMiss*p[i];
sum+=q[i];
}
for(i=0;i<NCELLS;i++)
q[i]=q[i]/sum;
return(q);
}