【发布时间】:2011-12-30 08:05:47
【问题描述】:
我的循环中的一个函数以某种方式改变了我正在迭代的值,我不确定如何。如果这描述得很糟糕,我很抱歉。
在这个for循环中
int k;
for( k = 0; k < 512; k++)
{
// Discardheader(d); // doesnt actually do anything, since it's a header.f
int databit = Getexpecteddata(d+4*k+1);
printf("%d ",k);
int transmitted = Datasample(&datastate, &datalength, d+4*k+2,dataerr,dataloc, databit);
printf("%d ",k);
Clocksample(&clockstate, &clocklength, d+4*k+3,clockerr, transmitted);
printf("%d \n",k);
}
我得到这个输出
16 16 16
17 17 17
18 18 18
19 19 19
20 20 20
21 1 1
2 2 2
3 3 3
4 4 4
所以一旦达到 21,Datasample 就会以某种方式更改 k 的值。d 是 char * d 类型,表示我在其中读取文件的缓冲区。更改输入文件不会改变在 21 发生切换的情况。这是datasample的代码:
int Datasample (int* state, int* length, char *d, int *type, int location, int data)
{
int match = 1; // if data sample and delayed tx match,
if ( ((d[0] >> location) & 1) != data)
{
match = 0;
if(data)
{
type[2]++;
}
else
{
type[1]++;
}
}
int ia;
for( ia = 7; ia>=0; ia--)
{
if ( ((d[0] >> ia) & 1) == *state) // finds an edge
{
*length++;
}
else
{
int distance, deviation,devflag=1; // distance the edge is from the sample point. should be about 4
if ( location > 3) // deviation is how far the distance then is from 4
{distance = location - ia;}
else
{distance = ia - location;}
deviation = abs(4-distance);
if( (deviation >= devmax) && match && devflag)
{
devflag =0;
if(data)
{
type[2]++;
}
else
{
type[1]++;
}
}
*state = ((d[0] >> ia) & 1);
*length = 1;
}
}
return ((d[0] >> location) & 1);
}
是什么导致 k 在达到 21 后回滚到 1?
提前致谢。我不知道我在做什么。
【问题讨论】:
-
不用想太多……k变量的类型是什么?它是一个 int 还是一个简单的 char(或最终是一个字节)?
-
哦,k 只是一个 int,我在上面声明了 2 行。对不起。
-
您的第三个参数是指向字符的指针 -
d是什么?你确定不是在那里践踏东西吗? -
d 是 char * d 类型,它是一个指向我读取文件的缓冲区的指针。
-
d有多大...?你是怎么分配的?