Pointers to modify a variable's value?

This is a discussion on Pointers to modify a variable's value? within the C++ Programming forums, part of the General Programming Boards category; In my book it shows how you can use a pointer to modify a variables value. But I see no ...

  1. #1
    _ Munkey01's Avatar
    Join Date
    Dec 2002
    Posts
    280

    Question 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?

  2. #2
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    >>>
    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.
    <<<

    When you pass a parameter to a function, it is passed by value, do whatever you like to it within the function and the value in the calling program will not be affected. Sometimes that is not what you want, pass a pointer to the parameter, and you have access to the memory location in the calling routine, so your function can alter it's value. There are other reasons, but try that for a start.

    >>>
    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?
    <<<

    Most compilers, when given a "const" will use a literal value when the code is converted to assembler, so changing the value of a const is, at best, undefined - don't do it. You can, however, cast away the const'ness of parameters to functions. If something is passed to a function as a const, you can use a const_cast<>() to get rid of the "const'ness". If you ever find you need to do this, I would suggest you have a design problem.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  3. #3
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078

    Re: Pointers to modify a variable's value?

    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.

    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.

  4. #4
    Banned frenchfry164's Avatar
    Join Date
    Oct 2001
    Posts
    1,552
    pointers at first may seem stupid, but given time you'll find tons of uses for them.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 05:23 AM
  2. Variables | Pointers in arguments really needed?
    By bobbelPoP in forum C++ Programming
    Replies: 40
    Last Post: 08-04-2008, 10:45 AM
  3. Problem with changing variables :pointers, functions
    By SyCo1234 in forum C Programming
    Replies: 16
    Last Post: 04-03-2005, 08:49 PM
  4. pointers as function variables
    By PHP in forum C++ Programming
    Replies: 2
    Last Post: 01-14-2003, 06:45 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21