Thread: do pointers affect what they point to directly?

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    41

    do pointers affect what they point to directly?

    if i feed pointers to a function and add 1 to them for example, does it change the actual value of the variable the pointer is pointing to? thanx
    In order of learned:
    HTML - Mastered
    CSS - Enough to use
    SSI - Mastered
    PHP - Advanced
    C/C++ - Current Project

  2. #2
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    if you dereference the pointer and add 1 to that, it will affect the value who's address was passed (so it will change the value outside of the function as well). If you just add to the pointer without dereferencing it, it will NOT affect the pointer outside of the function.

  3. #3
    Registered User
    Join Date
    Nov 2002
    Posts
    41
    and i dereference it how? sorry im a bit new to this lingo hehe
    In order of learned:
    HTML - Mastered
    CSS - Enough to use
    SSI - Mastered
    PHP - Advanced
    C/C++ - Current Project

  4. #4
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    dereferencing is when you access the value of the data pointer to by a pointer rather than the memory address of the variable pointed to. You usually do that with the unary operator*

    int a; // making an int
    int* PointerToAnint = &a; // Making a pointer to an int and initializing it to the address of a
    *PointerToAnint = 4; // dereferencing the pointer and setting the value of the variable it points to to 4.

  5. #5
    People Love Me
    Join Date
    Jan 2003
    Posts
    412
    Dereferencing is working with variables, through pointers. Typically, to manipulate variable values with a pointer, you'd do this:

    Code:
    ...
    int MyAge = 16;
    cout << MyAge << endl;
    int * pMyAge = &MyAge;
    *pMyAge = 21;
    cout << MyAge << endl;
    ...
    That should print out 16, then 21. I used the 'pMyAge' pointer to manipulate the 'MyAge' variable. This is dereferencing. the line '*pMyAge = 21;' means this:

    "Assign the value of 21 to the variable that 'pMyAge' is pointing to"

    [edit] My explanation > Polymorphic's
    Last edited by Krak; 01-13-2003 at 09:29 PM.

  6. #6
    In The Light
    Join Date
    Oct 2001
    Posts
    598
    howdy,
    if i feed pointers to a function and add 1 to them for example
    a pointer is an address not a variable, if you add 1 to a pointer it will no longer point to the address it originaly pointed to.
    pointer handling is a subject in itself.

    M.R.
    Last edited by itld; 01-13-2003 at 09:43 PM.
    I don't like you very much. Please post a lot less.
    Cheez
    *and then*
    No, I know you were joking. My point still stands.

  7. #7
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398

    The short answer to your question is "YES"

    When you pass a pointer to a function you can change the original value.

    A function can only return one value. So, if you need a function to change two values (say X and Y cooridinates), you can pass-in pointers to both and change both.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What is the point of pointers?
    By omnificient in forum C Programming
    Replies: 15
    Last Post: 12-15-2007, 06:09 PM
  2. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 06:58 PM
  3. wut is the point of pointers (no pun intended)
    By Geo-Fry in forum C++ Programming
    Replies: 2
    Last Post: 03-08-2003, 12:13 PM
  4. Replies: 2
    Last Post: 01-29-2003, 03:32 PM
  5. point lies in circle?
    By cozman in forum Game Programming
    Replies: 3
    Last Post: 12-20-2001, 04:39 PM