为什么赋值给一个int指针,什么时候我的C程序崩溃? [英] Why does my C program crash when assigning a value to an int pointer?

查看:185
本文介绍了为什么赋值给一个int指针,什么时候我的C程序崩溃?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想有一个函数从我的main()函数传递一些整数指针和值分配给他们。然而,我的程序赋值时崩溃。这里是我的code:

  INT computeMoveLocation为(int * R,为int * C,字符*板)
{
    //一些code在这里
    * R = 0; //这破坏了程序
    * C = 0;
}

我并不想改变的地址的指针 - 我试图改变的值的整数的被指出。不过,我显然是做错了。

任何帮助将大大AP preciated。

编辑:
下面是主要的相关code()。请让我知道我是否应该包括其他任何东西为好。

  INT的main()
{
    //初始化code
    // ...    而(1)
    {        开关(MACHINE_STATE)
        {
            案例COMPUTER_MOVE:
            {
               //检查行的三个X
               //检查列三个X
               //检查对角线为三个X
               //否则,将其他地方
               INT * R,* C;
               computeMoveLocation(R,C,板);
               computerMove(* R * C,板);
               preVIOUS_STATE = COMPUTER_MOVE;
               MACHINE_STATE = HUMAN_MOVE;
               打破;
            }            //其他情况
        } //结束开关
    } //结束时
} //主到底


解决方案

正在传递的指针,但你没有分配内存。因此,他们在内存中的一个随机位置指向。

  INT computeMoveLocation为(int * R,为int * C,字符*板)
{
    //一些code在这里
    * R = 0; //这破坏了程序
    * C = 0;
}

坏主:

  INT的main()
{
    INT * R;
    INT * C;
    字符*板;
    //糟糕,传递指针,但没有分配内存
    computeMoveLocation(R,C,板);
    返回0;
}

好主#1:

  INT的main()
{
    INT R = 5;
    INT C = 5;
    炭板='A';
    //精致,传递对堆栈变量的地址
    computeMoveLocation(安培; R,和C,和放大器;板);
    返回0;
}

好主#2:

  INT的main()
{
    为int * R =的malloc(sizeof的(INT));
    * R = 5;
    INT * C =的malloc(sizeof的(INT));
    * C = 5;
    字符*板=的malloc(sizeof的(炭));
    *板='A';
    //精细,传递指针指向堆
    computeMoveLocation(R,C,板);    免费(R);
    免费(C)
    免费(板);
    返回0;
}

I am trying to have a function take some integer pointers passed from my main() function and assign values to them. However, my program crashes when assigning values. Here is my code:

int computeMoveLocation(int* r, int* c, char* board)
{
    //some code up here
    *r = 0;    //This breaks the program
    *c = 0;
}

I'm not trying to change the address of the pointer--I'm trying to change the value of the integer being pointed to. However, I am apparently doing something wrong.

Any help would be greatly appreciated.

EDIT: Here is the relevant code from main(). Please let me know if I should include anything else as well.

int main()
{
    //initialization code
    //...

    while (1)
    {

        switch (MACHINE_STATE)
        {
            case COMPUTER_MOVE :
            {
               //check rows for three Xs
               //check columns for three Xs
               //check diagonals for three Xs
               //otherwise, move anywhere else
               int *r, *c;
               computeMoveLocation(r, c, board);
               computerMove(*r,*c, board);
               PREVIOUS_STATE = COMPUTER_MOVE;
               MACHINE_STATE = HUMAN_MOVE;
               break;
            }

            //Other cases
        }//end switch
    }//end while
}//end main

解决方案

You are passing in pointers but you didn't allocate memory. So they are pointing at a random location in memory.

int computeMoveLocation(int* r, int* c, char* board)
{
    //some code up here
    *r = 0;    //This breaks the program
    *c = 0;
}

Bad main :

int main()
{
    int *r;
    int *c;
    char *board;
    // bad, passing in pointers but didn't allocate memory
    computeMoveLocation(r, c, board); 
    return 0;
}

Good main #1:

int main()
{
    int r = 5;
    int c = 5;
    char board = 'a';
    // fine, passing address of variables on stack
    computeMoveLocation(&r, &c, &board); 
    return 0;
}

Good main #2:

int main()
{
    int *r = malloc(sizeof(int));
    *r = 5;
    int *c = malloc(sizeof(int));
    *c = 5;
    char *board = malloc(sizeof(char));
    *board = 'a';
    // fine, passing pointers that point to heap
    computeMoveLocation(r, c, board); 

    free(r);
    free(c)
    free(board);
    return 0;
}

这篇关于为什么赋值给一个int指针,什么时候我的C程序崩溃?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆