在c中练习使用指针时获得奇怪的输出

时间:2022-04-03 19:09:53

Here is my programme:

这是我的计划:

#include <stdio.h>

int tokenCopy(char* dest, const char* src, int destSize)
{
    int i;
    for (i = 0; i < destSize-1; i++) {
        if (src[i] != '\0' && src[i] != EOF && src[i] != ' '){
            dest[i] = src[i];
        } else {
            dest[i] = '\0';
            break;
        }
    }
    return i;
}

int main()
{
    char buff[5];
    int n = tokenCopy(buff, "This is a string", 5);
    printf("%d '%s'\n", n, buff);
}

I tried to use this to copy extract a string from a string to another string. With this test case, I supposed to get 4 'This'. But I get 4 'This�' instead. I know somehow my loop terminates an index later than it supposed to be, but I do not know how to fix it.

我试图用它来复制从字符串中提取字符串到另一个字符串。有了这个测试用例,我应该得到4'这个'。但我得到了4'This '。我知道我的循环终止一个索引比它应该的晚,但我不知道如何解决它。

I know there is built in function could help me with this situation, but I really want to find out the problem, thanks

我知道有内置功能可以帮助我解决这个问题,但我真的想找出问题,谢谢

2 个解决方案

#1


1  

The for loop will run until it's complete (the else case inside the loop will never happen), and then you just return from the function without adding the terminator to the destination string.

for循环将一直运行直到它完成(循环中的else情况永远不会发生),然后你只需从函数返回而不将终结符添加到目标字符串。

You need to add the terminator after the loop, not in the else inside the loop.

您需要在循环之后添加终结符,而不是在循环内的else中添加。


The fixed function should look like

固定功能看起来应该是这样的

int tokenCopy(char* dest, const char* src, int destSize)
{
    int i;
    for (i = 0; i < destSize-1; i++) {
        if (src[i] != '\0' && src[i] != ' '){
            dest[i] = src[i];
        } else {
            // Don't terminate here, just break out of the loop
            break;
        }
    }
    dest[i] = '\0';  // Terminate string
    return i;
}

Note that I also removed the EOF check, it's pretty much useless as no standard input function should put it in the array it writes to. There's also the problem that comparing the int value -1 (which is what EOF expands to) to the char value -1 will not work as you expect. If you check most input functions that returns characters, you will see that they return int.

请注意,我也删除了EOF检查,它几乎没用,因为没有标准输入函数应该将它放在它写入的数组中。还有一个问题是,将int值-1(这是EOF展开的内容)与char值-1进行比较将无法正常工作。如果检查大多数返回字符的输入函数,您将看到它们返回int。

#2


1  

It looks like your function does not insert the \0 in the end of the string. destSize value is 5, so once you copy the s character, the next iteration will stop the loop because i will be inferior to destsize - 1, so the else clause will not be processed.

看起来你的函数没有在字符串的末尾插入\ 0。 destSize值为5,所以一旦你复制了s字符,下一次迭代将停止循环,因为我将不如destsize - 1,所以else子句将不会被处理。

To bypass this, you should insert \0 after the for loop, like this :

要绕过这个,你应该在for循环后插入\ 0,如下所示:

int i;
for (i = 0; i < destSize-1; i++) {
    if (src[i] != '\0' && src[i] != EOF && src[i] != ' '){
        printf("Copy %c\n", src[i]);
        dest[i] = src[i];
    }
}

dest[i] = '\0';

return i;

Also, your condition src[i] != EOF is useless. You main function is not standard too, it should be int main(void) or int main(int argc, char *argv[]) and it must return a value.

另外,你的条件src [i]!= EOF是没用的。你的main函数也不是标准的,它应该是int main(void)或int main(int argc,char * argv []),它必须返回一个值。

#1


1  

The for loop will run until it's complete (the else case inside the loop will never happen), and then you just return from the function without adding the terminator to the destination string.

for循环将一直运行直到它完成(循环中的else情况永远不会发生),然后你只需从函数返回而不将终结符添加到目标字符串。

You need to add the terminator after the loop, not in the else inside the loop.

您需要在循环之后添加终结符,而不是在循环内的else中添加。


The fixed function should look like

固定功能看起来应该是这样的

int tokenCopy(char* dest, const char* src, int destSize)
{
    int i;
    for (i = 0; i < destSize-1; i++) {
        if (src[i] != '\0' && src[i] != ' '){
            dest[i] = src[i];
        } else {
            // Don't terminate here, just break out of the loop
            break;
        }
    }
    dest[i] = '\0';  // Terminate string
    return i;
}

Note that I also removed the EOF check, it's pretty much useless as no standard input function should put it in the array it writes to. There's also the problem that comparing the int value -1 (which is what EOF expands to) to the char value -1 will not work as you expect. If you check most input functions that returns characters, you will see that they return int.

请注意,我也删除了EOF检查,它几乎没用,因为没有标准输入函数应该将它放在它写入的数组中。还有一个问题是,将int值-1(这是EOF展开的内容)与char值-1进行比较将无法正常工作。如果检查大多数返回字符的输入函数,您将看到它们返回int。

#2


1  

It looks like your function does not insert the \0 in the end of the string. destSize value is 5, so once you copy the s character, the next iteration will stop the loop because i will be inferior to destsize - 1, so the else clause will not be processed.

看起来你的函数没有在字符串的末尾插入\ 0。 destSize值为5,所以一旦你复制了s字符,下一次迭代将停止循环,因为我将不如destsize - 1,所以else子句将不会被处理。

To bypass this, you should insert \0 after the for loop, like this :

要绕过这个,你应该在for循环后插入\ 0,如下所示:

int i;
for (i = 0; i < destSize-1; i++) {
    if (src[i] != '\0' && src[i] != EOF && src[i] != ' '){
        printf("Copy %c\n", src[i]);
        dest[i] = src[i];
    }
}

dest[i] = '\0';

return i;

Also, your condition src[i] != EOF is useless. You main function is not standard too, it should be int main(void) or int main(int argc, char *argv[]) and it must return a value.

另外,你的条件src [i]!= EOF是没用的。你的main函数也不是标准的,它应该是int main(void)或int main(int argc,char * argv []),它必须返回一个值。