Author |
Message |
Bak ?ls -s 0 in
Age:25 Gender: Joined: Jun 11 2004 Posts: 1826 Location: USA Offline
|
|
Back to top |
|
|
Mr Ekted Movie Geek
Gender: Joined: Feb 09 2004 Posts: 1379 Offline
|
Posted: Thu Jul 29, 2004 3:55 am Post subject: |
|
|
|
|
This is a very good question. All you people trying to learn C/C++ should attempt to understand what is going on here.
Instead of:
do:
(*intVec) = 0; // don't need the parens, but I like them in this case |
The reason is this. You are correct to pass the address to your pointer to the function. This way, the function actually knows where the calling code's pointer is stored. Inside your function, you de-reference the pointer-pointer, so you have a simple pointer to the structure, just as the calling code does. This is local data--only valid inside the function, lost when the function returns. If you want to affect the pointer of the calling code, you need to use the address passed to you, which is intVec--a pointer to the original pointer. _________________ 4,691 irradiated haggis!
|
|
Back to top |
|
|
Tommyhawk Newbie
Joined: Jul 14 2004 Posts: 14 Offline
|
Posted: Thu Jul 29, 2004 5:26 pm Post subject: |
|
|
|
|
Agreed good question.
Alternatively he could have done
|
|
Back to top |
|
|
Mr Ekted Movie Geek
Gender: Joined: Feb 09 2004 Posts: 1379 Offline
|
Posted: Thu Jul 29, 2004 6:33 pm Post subject: |
|
|
|
|
True. Same thing. But I find using the original param name shows more clearly that you are affecting something outside the scope of the function.
|
|
Back to top |
|
|
Bak ?ls -s 0 in
Age:25 Gender: Joined: Jun 11 2004 Posts: 1826 Location: USA Offline
|
Posted: Thu Jul 29, 2004 7:52 pm Post subject: |
|
|
|
|
Tommyhawk wrote: | Agreed good question.
Alternatively he could have done
|
iv is pointing to the actual IntVec struct in memory. If I do *iv = 0 won't that just change the data in the struct, rather than the pointer?
And If I do it after I delete it, aren't I modifing a struct which has been deleted, and could be being used by other variables?
Doing (*intVec) = 0 makes sense Ekted, thanks.
pointerDiagram.PNG - 4.13 KB
File downloaded or viewed 46 time(s)
|
|
Back to top |
|
|
Mr Ekted Movie Geek
Gender: Joined: Feb 09 2004 Posts: 1379 Offline
|
Posted: Thu Jul 29, 2004 8:21 pm Post subject: |
|
|
|
|
Yes, you are right Bak. I take back what I said. I spoke too quickly. "*iv = 0;" is in fact an error, and will not even compile.
|
|
Back to top |
|
|
Tommyhawk Newbie
Joined: Jul 14 2004 Posts: 14 Offline
|
Posted: Fri Jul 30, 2004 12:30 am Post subject: |
|
|
|
|
Meh mb. I had the arrows on the diagram in my head in slightly different places
|
|
Back to top |
|
|
|