问题
Say I have something like:
function2(int *hello) {
//something
}
function1(int *hello) {
function2(&hello);
}
void main() {
int hello = 0;
function1(&hello);
}
How do I make it so that function2
can change the original value declared in main
?
回答1:
Change this code:
function1(int *hello) {
function2(&hello);
}
to:
function1(int *hello) {
function2(hello); // <-- no "&" in this call!
}
Then you can do this:
function2(int *hello) {
*hello = 123; // <-- dereference pointer hello
}
回答2:
You are making a mistake in the function1
where your pass the address of the pointer to your int hello
in the main function. you pass the pointer forwart to the function2
and the dereference it in there.
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 ;
}
You are also incorrectly declaring your main function. It should be declared as int main() with a return statement.
回答3:
As mvp reply it is correct answer but i am here just explaining something, which will help you to understand, why your code is not working.
First let me explain operator you used
1] Ampersand Operator (&)
This operator helps you to get reference/address of variable
As soon as we declare a variable, the amount of memory needed is assigned for it at a specific location in memory, As we generally do not actively decide the exact location of the variable within the operating system during run time but some time we need to know address of our variable like here
You want to know the address of variable to assign it's address to pointer
The address that locates a variable withing memory is what we call a reference
to that variable Which we can get using Reference variable.Dereference Operator (*)
As we know that pointer is the variable which will store that address of the another variale, using pointer we can directly access the value stored in the variable which it points.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
Now back to your code
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]
}
Solution is as suggested
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);
}
回答4:
The "&" operator means "address of", so your function 1 is trying to call function2 with the address of "hello" rather than the address hello contains.
Responding to a different question I said this:
Pointers are variables which store a number, like any other, but because you tell the compiler it is a pointer, the compiler allows you to use that value as an address of things in memory and the language provides "dereferencing" as a way of saying "the value AT the address this variable describes".
Imagine you're going to the airport to catch a flight. You grab a post-it note and write down your flight number, and you take a second post-it with you for your gate.
The first post it is a "flight*" pointer and your second is a "gate*" but right now the gate* pointer is empty.
When you get to the airport, you look up your flight on the board and jot down the gate number. "3A". Now your gate* post-it is valid.
But the post-it itself is NOT your gate, it just points to it: you still have to "dereference" the post-it note to get to your flight - that is, walk across the airport to gate 3A :)
When you called function1
you took the address of the variable in main. All you need to do is forward it to function2. What your code was trying to do was make a note of which post-it not the gate number was on, rather than just looking at the post-it note.
Solution
#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;
}
You can see a live demo of this code on ideone.com here.
Output looks like this:
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.
来源:https://stackoverflow.com/questions/19602300/c-pointers-when-new-functions-are-called