首页 > 解决方案 > 指针值修改行为

问题描述

我是 C 程序员初学者,我遇到了一些我无法完全理解的事情。

这是我的整个代码:

#include <curses.h>
#include <unistd.h>

#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <winuser.h>

#define DELAY 300000

int x = 3, y = 20;
int max_y = 0, max_x = 0;
int next_x = 0;
int directionX = -1;
int directionY = 0;

int main(int argc, char *argv[])
{
    initscr();
    noecho();
    curs_set(FALSE);

    getmaxyx(stdscr, max_y, max_x);

    while(1)
    {
        // drawing on the map
        clear();
        mvprintw(y, x, "o");
        refresh();
        usleep(DELAY);

        // make next step
        next_x = x + directionX;

        //set next step directionX
        if (next_x >= max_x || next_x < 0)
        {
            directionX*= -1;
        }
        else
        {
            x+= directionX;
        }
    }

    endwin();
}

从我的调试中,这张图片描述了我无法完全理解的问题:

在此处输入图像描述

请有人向我解释一下,如何将指针值分配给-1并接收+1?

标签: c

解决方案


推荐阅读