Greeting XianBin,
Pointers can be somewhat confusing, but let's see if we can go through this code one step at a time. It may help the understanding of how it works, and how useful these techniques can be.
Let's disect the functions first:
int *fun_num() {
int b = 123;
return &b;
} » What this does, is it creates an integer
b, and returns the memory address of this variable. That's why the function's return-type is "
int *".
char *fun_char() {
char *b = "123";
return b;
} » This does almost the same thing, as it creates a variable "
char *" and returns it. Not the memory address since an array name is a pointer. If you were to send the memory address, then you would need to change the return-type to "
char **". Oh, and the
b variable would need a real block of memory, since right now it is only staticly created.
Now moving inside main, lets take a look at our routines:
» These are your local variables. One is a pointer to an integer, and the other, a char array [or pointer]. The "
char *" array only needs data, though the integer pointer needs the memory address to look at the data we send.
» Ah, the messy stuff. Well, this is quite simple, so I'll explain it as simple as possible. This contains
pfun. If you may know,
pfun is not the actual data itself, its the address. Remember in fun_num() we returned the address of our local variable
b, well that address will be sent directly to
pfun. Now in order to read the data passed through the address we would be smart to use the unary operator "
*".
The reason you need the unary (*) operator, is because we sent the memory address to our function. Let me clarify. If a variable contains data here (d) and its memory address is here (m), I would need to get to (m)->(d) to access the data from the address sent. Likewise, the * operator allows me to read the data from the address. Using the * will point to the data instead of looking or modifying the address. So overall,
pfun is our address, and
*pfun is our data from our address.
» This is somewhat the same, but different. Remember that this will only contain the data without touching the address. This syntax is correct since with the previous line of code we sent address to address, but now we are sending data to data.
» Ah, we are reading our data here.
cout << *fun_num() << endl;
» Same address, doesn't matter where we call it. Reading the same data as
*pfun.
» Remember,
pchar is now reading whatever fun_char() holds. They aren't exactly linked like pointers and addresses, but you sent the
data from our function to our variable. As long as that occurs, the data will still be there. It can't be delete either, since
b isn't exactly our property to touch in fun_char(). It's dynamically out there somewhere.
cout << fun_char() << endl;
» Same data, but not same address. If this function were to have allocated a memory block we could possibly send the address anywhere we want.
Conclusion
Though it isn't recommended to do a statement like so:
I don't want to be harsh or anything telling you it isn't safe or anything like that. Using malloc() may save you some headache down the line, but I'll explain that when you're ready or have learned it and/or have further questions about it.
I hope this information has helped you in great detail. If you have any further questions, please feel free to ask.
-
Stack
Overflow