c - 仅分配一次时,3 个字符串获得相同的值

标签 c string loops

好吧,我不知道为什么会发生这种情况。

这是我正在处理的函数。

while (map[x] && height <= box.size)
{
    width = 0;

    while (width <= box.size && height <= box.size)
    {
        if (map[x] == '.' || map[x] == 'x')
        {
            main_box[x] = 'x';
            n_of_fill++;
            width++;
        }
        if (map[x] == 'o')
            break;
        x++;
    }
    //To move a line down and start at the beginning of that line
    x = x + true_y + 1 - width;
    box.size = width;
    height++;
}
printf("3 \n\n%s\n\n%s\n\n%sfill = %i, width = %i, height = %i\n\n", main_box, map, tempmap, n_of_fill, width, height);

我传递了一个 map ,声明了另外 2 个 map (tempmap 和 main_box)。一开始我将 tempmap 和 main_box 设置为 map,该函数按照其指示执行操作,(甚至很难,但它并不正确)...但现在在代码末尾,当我打印所有 map 时,所有 map 是一样的吗??

当在循环中时,我只更改 map ,并且没有将 main_box 或 tempmap 设置为 map ,那么为什么它们在函数末尾都相同?!!

这是完整的代码(要小心很长且写得很糟糕的代码) http://pastebin.com/SdnJYPqP

任何帮助都会很棒,如果我是一个彻头彻尾的白痴,我很抱歉......

最佳答案

我真是太傻了。 main_boxtempmap 是指向 map 的指针,所以当我更改 map 并调用所有其他函数时,它调用的是相同的事物。谢谢您的帮助!归功于巴马尔

关于c - 仅分配一次时,3 个字符串获得相同的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36654492/

相关文章:

JavaScript - 使用数据创建数组而不循环

c++ - execv,选择并阅读

python - 替换列(字符串)中的值时无法覆盖现有数据帧值

java - 了解 float 和无限循环

java - 在 Java 运行时评估代码

string - 如何使用 Flutter TextFormField 在行开头和结尾进行缩进和缩进

javascript - JavaScript 循环中的 .length -1 与 .length

c++ - Linux gdb 'examine' 行为

java - 使用 JNI 使用 Windows 命名管道的 Java 程序的 IPC

c - 错误 : 'Math' undeclared first use in this function