It is required to define a function from the outside and change the value of C in the main function.
Steps:
1. Call a function. If you want to modify the value of the variable in the main function externally, you need to pass the address of this value to the function. & C
2. In an external function, the pointer is used to modify the value pointing to C.
1 void changec (char * P) {2 3 // pass the pointer. modify the value of C 4 5 * P = 9; 6 7} 8 9 int main (INT argc, const char * argv []) {10 11 @ autoreleasepool {12 13 // define a char type variable C14 15 char c = 10; 16 17 // call the changec function. pass the c Address to the function, 18 19 changec (& C); 20 21 nslog (@ "% I", c); 22 23} 24 25 return 0; 26 27}
Relationship in memory
Send the STR address (the pointer is the address) to the function. You can modify the value of the bucket pointed to by STR through str1.
# Import <Foundation/Foundation. h> void changestr (nsstring ** str1) {* str1 = @ "hi";} int main (INT argc, const char * argv []) {@ autoreleasepool {// defines a pointer pointing to the address with the content @ "hello". Str itself is a pointer nsstring * STR = @ "hello"; // when calling a function, modify the STR content changestr (& Str); nslog (@ "% @", STR);} return 0 ;}
Object-C pointer to pointer