C 简易指针示例
C easy pointer example
谁能解释为什么程序的结果是"5 3"。我需要显示程序如何工作的步骤的简短列表。如果我的问题太简单,请原谅我,我只是初学者。代码如下:
#include <stdio.h>
float x = 4.5;
float y = 2;
float proc(float z, float *x)
{
*x *= y;
return z + *x;
}
int main()
{
float x, y, *z;
x = 2.5; y = -2; z = &x;
y = proc(y, z);
printf("%f %fn", x, y);
return 0;
}
int main()
{
float x, y, *z;
x = 2.5; y = -2; z = &x;
y = proc(y, z);
/*
inside the proc function, you are passing -2 and the address of x in to proc.
*x = *x * y // y = 2 which is the global variable defined earlier and *x = 2.5 which is the passed in variable.
*x = 5 after the multiplication.
z + *x = 3 will be returned and passed in to the variable y defined inside the main function.
*/
printf("%f %fn", x, y);
return 0;
}
相关文章:
- 删除指向指针的指针是运行时错误吗
- 对于C++中使用智能指针的指针算术限制,有没有一种变通方法
- 为什么我收到"从常量指针到指针的转换无效?
- 指向C++中的指针的指针
- C++ 传递指向对象的指针的指针,无法访问成员
- 如何创建指向函数指针的指针?
- 指针投射:指针有价值吗?
- 将唯一指针的指针传递给采用双指针的函数
- 这些指针的含义,指针到指针,函数指针和数组指针
- 获取指向指针的指针元素 (c++)
- 带有空指针的指针 c++ 的 2D 数组
- 智能指针(唯一指针)和引用
- 从 unique_ptr::get 发送指针作为指针引用进入函数
- 为什么具有指针参数的成员函数需要指向指针的指针?
- 通过双指针分配指针
- 在指针到指针场景中使用std::shared_ptr
- 正确的方法是什么?调用指针到指针到指针内部的函数?
- C++ 常量用于指针指向指针
- 指向数组中指针的指针
- 无法向上转换指针到指针参数