Pointers to modify a variable's value?
In my book it shows how you can use a pointer to modify a variables value. But I see no reason of why this would be important.
Also, if you declared a variable as a constant and you modified it's value using a pointer, would this work or would you get a compile error?
Edit: Also what would you call the C++ Bible?
Re: Pointers to modify a variable's value?
Quote:
Originally posted by Munkey01
In my book it shows how you can use a pointer to modify a variables value. But I see no reason of why this would be important.
There are many many many uses for this. For instance, passing a pointer or reference to a function allows you to edit the data pointed to from within the function (which you wouldn't be able to do normally). Also, it allows you to manage dynamic memory allocation.
Other reasons are that you may need to be able to access and modify an object from multiply other objects. One way of handling this is having a pointer in each object to the common object. Then all of those objects can access and modify it.
Quote:
Originally posted by Munkey01
Also, if you declared a variable as a constant and you modified it's value using a pointer, would this work or would you get a compile error?
No, you wouldn't get a compiler error, but you will get undefined results. Sometimes it can just edit a temporary value, sometimes it can cause a runtime error, other times it can work fine.