gpt4 book ai didi

c - C 中字符串的自增

转载 作者:行者123 更新时间:2023-11-30 21:37:22 27 4
gpt4 key购买 nike

当我们尝试在 C 中递增字符串时会发生什么?

#include <stdio.h>

void foobar(char *str1, char *str2)
{
while (*((str1++)+6) == *((str2++)+8));
}

int main(){
char str1[] = "Hello World";
char str2[] = "Foo Bar Bar";

foobar(str1,str2);

printf``("%s %sn",str1, str2);

return 0;
}

输出是:

Hello World Foo Bar Barn

最佳答案

我不知道你想做什么,但你的函数 foobar 什么也不做,因为你所做的只是迭代局部变量 str1 上的字符串 - 对它的更改不会被传回到主要。

嗯,这并不完全正确。 Foobar 可能会超出字符串的末尾并导致其现在的编写方式崩溃。

关于c - C 中字符串的自增,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33870995/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com