说我有类似的东西:
function2(int *hello) {
//something
}
function1(int *hello) {
function2(&hello);
}
void main() {
int hello = 0;
function1(&hello);
}
我如何使它function2
可以更改中声明的原始值main
?
更改此代码:
function1(int *hello) {
function2(&hello);
}
到:
function1(int *hello) {
function2(hello); // <-- no "&" in this call!
}
然后你可以这样做:
function2(int *hello) {
*hello = 123; // <-- dereference pointer hello
}
您在主函数function1
中将指针的地址传递给您的地方犯了一个错误。int hello
您将指针向前传递给 thefunction2
并在其中取消引用它。
function2(int *hello) {
*hello = 123 ;
}
function1(int *hello) {
function2(hello); //pass the pointer on to function2
}
int main( void ) {
int hello = 0;
function1(&hello);
return 0 ;
}
您还错误地声明了您的主要功能。它应该被声明为 int main() 并带有 return 语句。
作为mvp回复 it is correct answer but i am here just explaining something, which will help you to understand, why your code is not working.
This operator helps you to get reference/address of variable
You want to know the address of variable to assign it's address to pointer
的地址,就像这里一样。在内存中定位变量的地址是我们称之为变量的地址,reference
我们可以使用引用变量获得。解引用运算符 (*)
正如我们所知,指针是将存储另一个变量的地址的变量,使用指针我们可以直接访问存储在它指向的变量中的值。To do this we simply have to precede the pointer's identifier with an asterisk (*), which acts as deference operator and that can be literally translated to value pointed by
function2(int *hello) { // Here you create function that accept address of variable (You will get address of hello pointer 101 NOT 100 of variable assign in your MAIN)
//something
}
function1(int *hello) {// Here you create function that accept address of variable (Here it will get address of hello which is 100)
function2(&hello); // Here you again pass address of your pointer hello (Which may be 101) [BAD]
}
main {
int hello = 0; // Let say it's address is 100
function1(&hello); // Here you pass address of your variable(which is 100) [GOOD]
}
function2(int *hello) {
//Change your value here
*hello = 123;
}
function1(int *hello) {
function2(hello); // It will pass 100 (address of your variable hello assign in MAIN
}
main {
int hello = 0;
function1(&hello);
}
“&”运算符表示“地址”,因此您的函数 1 正在尝试使用“hello”地址而不是 hello 包含的地址调用函数 2。
回答一个不同的问题,我这样说:
指针是存储数字的变量,就像任何其他变量一样,但是因为您告诉编译器它是一个指针,编译器允许您将该值用作内存中事物的地址,并且该语言提供“取消引用”作为一种说法“此变量描述的地址处的值”。
想象一下,你要去机场赶飞机。你拿一张便利贴写下你的航班号,然后随身携带第二张便利贴作为登机口。
第一篇文章是“飞行*”指针,第二篇文章是“门*”,但现在门*指针为空。
当你到达机场时,你在板上查看你的航班并记下登机口号码。“3A”。现在你的门*贴是有效的。
但是便利贴本身不是您的登机口,它只是指向它:您仍然必须“取消引用”便利贴才能到达您的航班 - 也就是说,穿过机场到 3A 登机口 :)
当您调用时,function1
您获取了 main 中变量的地址。您需要做的就是将其转发给 function2。您的代码试图做的是记下哪个便利贴而不是门号,而不仅仅是查看便利贴。
解决方案
#include <stdio.h>
void function1(int*); // so the compiler knows what the function looks like.
void function2(int*);
int main() {
int varInMain = 0; // lets call it something distinct
printf("varInMain starts with %d, it's location in memory is %p.\n",
varInMain, &varInMain);
function1(&varInMain);
printf("varInMain is %d after calling function1.\n", varInMain);
return 0;
}
void function1(int* func1ptr) {
printf("function1: func1ptr points to memory location %p, which contains %d.\n",
func1ptr, *func1ptr);
*func1ptr = 1010;
function2(func1ptr);
}
void function2(int* func2ptr) {
printf("function2: func2ptr points to memory location %p, which contains %d.\n",
func2ptr, *func2ptr);
*func2ptr = 123;
}
输出如下所示:
varInMain starts with 0, it's location in memory is 0xbfef2fdc.
function1: func1ptr points to memory location 0xbfef2fdc, which contains 0.
function2: func2ptr points to memory location 0xbfef2fdc, which contains 1010.
varInMain is 123 after calling function1.