说我有类似的东西:
function2(int *hello) {
//something
}
function1(int *hello) {
function2(&hello);
}
void main() {
int hello = 0;
function1(&hello);
}
如何设置function2
以更改main
中声明的原始值?
答案 0 :(得分:2)
更改此代码:
function1(int *hello) {
function2(&hello);
}
为:
function1(int *hello) {
function2(hello); // <-- no "&" in this call!
}
然后你可以这样做:
function2(int *hello) {
*hello = 123; // <-- dereference pointer hello
}
答案 1 :(得分:2)
在function1
中,您在主函数中传递指向int hello
的指针的地址时出错。您将指针转发传递给function2
并在其中取消引用它。
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 ;
}
您也错误地声明了您的主要功能。它应该使用return语句声明为int main()。
答案 2 :(得分:2)
作为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);
}
答案 3 :(得分:0)
“&amp;” operator表示“地址”,所以你的函数1试图用地址“hello”调用function2,而不是地址hello包含。
回答另一个问题I said this:
指针是存储数字的变量,与任何其他指针一样,但是因为你告诉编译器它是一个指针,编译器允许你使用该值作为内存中的事物的地址,并且语言提供“解除引用”作为说“这个变量所描述的地址的价值”的方式。
想象一下,你要去机场赶飞机。你抓住便条纸并写下你的航班号,然后你带着第二张贴在你的门上。
第一个帖子是“flight *”指针,第二个是“gate *”,但现在门*指针是空的。
当你到达机场时,你在船上查找你的航班并记下门号。 “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;
}
您可以看到live demo of this code on ideone.com here。
输出如下:
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.