我刚开始用指针,我有点困惑.我知道&一个变量的地址,它*可以在指针变量前面使用,以获取指针指向的对象的值.但是当您使用数组,字符串或使用变量的指针副本调用函数时,情况会有所不同.在所有这些内部很难看到逻辑模式.
什么时候应该使用&和*?
我指点不是很好,但我必须在现场学习.如果我的理解对我有用,这些都应该是以下有效的陈述.
int* a;
int b = 10;
a = &b;
(*a) = 20;
(*a) == b; //this should be true
Run Code Online (Sandbox Code Playgroud)
如果你有这样的功能:
void copy(int* out, int in) {
*out = in;
}
int m_out, m_in;
copy(&m_out, m_in);
m_out == m_in; // this should also be true
Run Code Online (Sandbox Code Playgroud)
但是我看到了这样的功能
create(float& tp, void* form, char* title);
Run Code Online (Sandbox Code Playgroud)
我理解void指针,它可以转换为任何东西,我理解字符指针基本上是ac样式字符串.
我不明白第一个参数,这是某种类型的地址,让我们说一个浮点数,但它可以是任何东西,结构,一个int等.
那里发生了什么?
#include<stdio.h>
#include<conio.h>
int f(int & ,int );//function prototype
main()
{
int x,p=5; //p is initialized to 5
x=f(p,p);
printf("\n Value is : %d",x);//print the value of x
getch();
}
int f (int & x, int c)
{
c=c-1;
if (c==0) return 1;
x=x+1;
return f(x,c) * x; //recursion
}
Run Code Online (Sandbox Code Playgroud)
输出:6561
谁能向我解释程序的流程。这个问题来自我无法理解的门。似乎该函数以p = 5的值调用。它被int&x捕获在函数f中,问题出在这里。是值,即5是存储在x还是x的地址中。