Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在C中使用函数和指针更改int值_C - Fatal编程技术网

如何在C中使用函数和指针更改int值

如何在C中使用函数和指针更改int值,c,C,我不想将p值更改为扫描的值,我的代码有什么问题?此代码演示了更改数字的正确方法和错误方法 函数get\u number\u A不会对其参数进行有意义的更改,因为C对其参数使用“pass by copy” 函数get_number_B将对其参数进行有意义的更改,因为传递了指向变量的指针 int boom_number; get_boom_number(boom_number); void get_boom_number(int *p) { int x; p

我不想将p值更改为扫描的值,我的代码有什么问题?

此代码演示了更改数字的正确方法和错误方法

函数
get\u number\u A
不会对其参数进行有意义的更改,因为C对其参数使用“pass by copy”

函数
get_number_B
将对其参数进行有意义的更改,因为传递了指向变量的指针

    int boom_number;
    get_boom_number(boom_number);
    void get_boom_number(int *p)
{
    int x;
    p = &x;
    if(scanf("%d",&x)== 0)
        printf("Input Error!");
    else
        *p = x;
    return;
}

警告,你;重新存储局部变量的地址,这不是静态存储持续时间!!您已经设置了
p=&x*p=x修改它指向的内容将修改
x
,而不是
*p
。我应该如何修复它@souravghosh您应该删除行
p=&x@PaulOgilvie,同样的问题
void get_number_A(int x)
{
    x = 5; // This change will NOT happen outside of this function.
}

void get_number_B(int* p)
{
    *p = 7; // This change will happen outside of this function.
}

int main(void)
{
    int number = 0;

    get_number_A(number);
    printf("A.) The number is: %d; it was NOT modified.\n", number);

    get_number_B(&number);
    printf("B.) The number is: %d; it was SUCCESSFULLY modified.\n", number);

    return 0;
}