【发布时间】:2012-09-21 02:51:15
【问题描述】:
为什么这不起作用?
short pStringCt = 0;
const char* constInBuf = ibuf;
int j;
for (j = 0; j < i; j++)
{
/*Detect when there's a space in the buffer and grab word*/
if (ibuf[j] == ' ' ||
ibuf[j] == '|' ||
j == i - 1)
{
int k;
for (k = j - 1; k >= 0; k--){
if (k == 0 || ibuf[k] == ' ' || ibuf[k] == '|')
{
if (pStringCt > 0){
pString[pStringCt] = strndup(constInBuf + k + 1, j - k + 1);
printf("-pString at %d is: %s\n", pStringCt, pString[pStringCt]);
pStringCt += 1;
}
if (pStringCt == 0){
pString[pStringCt] = strndup(constInBuf+k, j-k + 1);
printf("-----pString at %d is: %s\n", pStringCt, pString[pStringCt]);
pStringCt+=1;
}
printf("%d is pStringCt\n", pStringCt);
break;
}
}
}
在 break 语句之后 pStringCt 的值没有增加。换句话说,pStringCt 在方法开始时为 0。然后,它在与其相关的两个 if 语句之后增加到一个。但是在break语句之后,它重置为零......
【问题讨论】:
-
你有没有想过调试你的程序?
-
我曾经做过。 printf 语句总是在增量前返回 0,在增量后返回 1。但他们从不从 1 开始,然后返回 2,等等。
-
请粘贴整个代码并给我们您当前代码的输出。我看到那里有很多打印内容,您可以通过在第一个语句之前打印值来简单地调试您的代码中断语句。
标签: c for-loop increment break short