Welcome everyone to help me correct my mistakesIn a function like this, we can introduce external variables for use within the function
#include<stdio.h>void function(int n){ int m = n*n; printf("%d",m);}int main(){ int n = 2; function(n); return 0;}// Code 1
Output:
4
However, what if we try to modify the value of n directly within the function?
#include<stdio.h>void function(int n){ n = n*n; printf("%d\n",n);}int main(){ int n = 2; function(n); printf("%d",n); return 0;}// Code 2
Output:
42
As we can see, even though we modified the value of n within the function, the n outside the function remains unchanged. This is because the n inside the function is actually a variable defined within the function. The call on line 10 function(n) simply assigns the value of the external n to the internal n, which is equivalent to
#include<stdio.h>int main(){ int a = 2; int b = a; // Assign the value of a to b b = b + 2; // Modify b printf("%d %d",a,b); return 0;}// Code 3
Output:
2 4 // Modifying b only changes b itself, not a
Returning to Code 2, if we want to modify the value of n within the function, there are two methods:1. Set n as a global variable
#include<stdio.h>// Define n outside the main function so that all functions can call and modify nint n = 2;void function() // No need to write formal parameters{ n = n*n; printf("%d\n",n);}int main(){ function(); // No need to pass n here printf("%d",n); return 0;}// Code 4
Output:
44
2. Use pointersWe will modify Code 3 using pointers to try to change both a and b’s values simultaneously
#include<stdio.h>int main(){ int a = 2; int* b; b = &a; *b = *b + 2; // Modify the value of *b printf("%d %d",a,*b); return 0;}// Code 5
Output:
4 4
As we can see, when we modify *b, the value of a is also changed. Why is that?We know that the content of an int is an integer, the content of a float is a floating-point number, and the content of a char is a character. Similarly, the content of a pointer variable is actually a memory address.In Code 5, if we directly output b, changing
printf("%d %d",a,*b);
to:
printf("%d %d",a,b);
the output should be an integer 4 and a string of unfamiliar numbers, which not only represents b itself but also the address of a &a.
4 6422296
b = &a; // This line assigns the address of a to b.
Some may ask, what does adding an asterisk * in front of b mean?Here we need to distinguish a common mistake: the asterisk in int* b and *b does not mean the same thing.In int* b, the * is used to declare a pointer variable b that points to an int type. In simple terms, it tells the compiler:“I want to define a variable b, which is a pointer variable used to represent an address, and the address should store an int.”Thus, we can see that the asterisk is clearly closer to int and has nothing to do with b.!!! But note: int* b, c; means b is a pointer, while c is a regular int.In *b, the asterisk is directly next to b, what does that mean?In technical terms, this is called dereferencing b, which means accessing the content stored at the address represented by b.In Code 5, I assigned a‘s address &a to b, so now b equals a‘s address, and *b equals a.Now let’s try to modify Code 2 using pointers:
#include<stdio.h>void function(int* n) // Here n can be renamed to anything, it can modify the external n{ *n = (*n) * (*n); printf("%d\n",*n);}int main(){ int n = 2; function(&n); // Pass the address of n printf("%d",n); return 0;}// Code 6
Output:
44
As we can see, we can now directly modify n within the function.Congratulations, you have learned the basic application of pointers