【发布时间】:2015-06-16 01:04:30
【问题描述】:
我有这样的代码:
typedef struct _Statistics {
Some code here
} Statistics;
void function1(char *string, Statistics *statistic){
Some code here
function1(string1, statistic);
}
int main(){
Statistics statistic;
function1(string, &statistic);
}
这可能是一个愚蠢的问题,但我不完全理解指针: 我明白为什么我在主函数中使用&,&发送变量统计的地址,以便在function1中我可以修改它。但是为什么我不在递归函数中使用 & 呢?
【问题讨论】:
-
因为它已经是一个指针。 main 中的
statistic是一个实际对象。调用function1(string, &statistic);将指针 传递给function1。所以当function1调用自己时,它可以直接使用main()给出的指针。 -
将你的函数声明为
void function1(char *string, Statistics *ptr),你就会明白为什么递归调用是function1(string1, ptr);
标签: c pointers recursion struct argument-passing