Do I understand pointers correctly?

This is a discussion on Do I understand pointers correctly? within the C++ Programming forums, part of the General Programming Boards category; Ok, I'm trying to see if I understand points correctly. Here is how I understand them: 1) Pointers act as ...

  1. #1
    C(++)(#)
    Join Date
    Jul 2004
    Posts
    309

    Do I understand pointers correctly?

    Ok, I'm trying to see if I understand points correctly. Here is how I understand them:

    1) Pointers act as a variable that points to the memory adress of another variable:
    Code:
     
    int a = 10;
    int *b= &a;
    2) When dereferenced, you can output the value of the variable the pointer references:
    Code:
    std::cout<<*b<<endl;
    OUTPUT: 10
    3) When not dereferenced, you can output the memory adress of the variable the point references. Which can later be used to read the memory of a variable from another application (such as in a minesweeper cheat):
    Code:
    std::cout<<b<<endl;
    OUTPUT: *bunch of hex*
    4) Pointers can be used to pass by reference, so that a variable can be modified by a function without having to return it - allowing for the modification of multiple variable within a function:
    Code:
     
    int inc(int a, int *b)
    {
        a++:
        *b = 20;//Is this legal?
        return a;
    }
    int f = 0;
    int g = 1;
    inc(f, &g)//f is now 1, g is now 20

    Ok, that's all I can think of right now. However, I want to know if I am right on those four points, and if there is anything else I should know.


    EDIT: Yeah yeah, this is the 5 trillionth post on pointers, I know. However, unless I ask myself I won't be sure
    Last edited by 7smurfs; 01-04-2006 at 11:29 AM.
    To code is divine

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,496
    Yes
    Yes
    Yes and No - reading another processes memory is far more involved than just declaring a pointer.
    Yes.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    >> inc(f, &g)//f is now 1, g is now 20
    The comment is incorrect. After that call f is still 0 because it is passed by value to the inc function. It would work if you had:
    Code:
    f = inc(f, &g)//f is now 1, g is now 20
    It would also work if a was a reference parameter instead.

  4. #4
    C(++)(#)
    Join Date
    Jul 2004
    Posts
    309
    Quote Originally Posted by Salem
    Yes and No - reading another processes memory is far more involved
    Yeah, I understand that it needs more than just a pointer - I was just using that as an example of when one might be used

    Ok, thanks Salem! Now I can rest easily


    EDIT:
    Quote Originally Posted by Daved
    >> inc(f, &g)//f is now 1, g is now 20
    The comment is incorrect. After that call f is still 0 because it is passed by value to the inc function. It would work if you had:
    Code:
    f = inc(f, &g)//f is now 1, g is now 20
    It would also work if a was a reference parameter instead.
    Ok, I thought I needed to do that, but decided not to for reasons I no longer remember.
    To code is divine

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    1) Pointers act as a variable that points to the memory adress of another variable:
    A variable that is a pointer type stores a value just like any other variable. The value it stores is the address of another variable.

    2) When dereferenced, you can output the value of the variable the pointer references:
    When you dereference a pointer variable, you are using the address stored in the pointer variable to access the value at that address in memory.

    3) When not dereferenced, you can output the memory adress of the variable the point references.
    Like most variables, you can output the value stored in the pointer, which is an address(although for pointers to type char it is trickier).
    Last edited by 7stud; 01-04-2006 at 11:42 PM.

  6. #6
    Registered User
    Join Date
    Jul 2003
    Posts
    450
    1) Pointers act as a variable that points to the memory adress of another variable:
    Code:


    int a = 10;
    int *b= &a;
    Pointers act as a variable that points to a memory adress that may be another variable.

    Code:
    WIKI *widget = new Pointer_Wiki_Widget();
    in this case the WIKI type pointer called widget points to an objects adress in memory. Is this really a variable?

  7. #7
    C(++)(#)
    Join Date
    Jul 2004
    Posts
    309
    Quote Originally Posted by curlious
    Pointers act as a variable that points to a memory adress that may be another variable.

    Code:
    WIKI *widget = new Pointer_Wiki_Widget();
    in this case the WIKI type pointer called widget points to an objects adress in memory. Is this really a variable?
    That's the point I was forgetting to add.

    At any rate, I knew that so as far as I'm concerned, I'm good to go unless someone wants to point out anything else I forgot/didn't know.
    To code is divine

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. man -k (how to use correctly)
    By cus in forum Linux Programming
    Replies: 2
    Last Post: 01-14-2009, 10:00 AM
  2. Did I Install the Platform SDK Correctly?
    By bengreenwood in forum C++ Programming
    Replies: 7
    Last Post: 07-14-2008, 09:33 AM
  3. strange error -- COM DLL is not installed correctly?
    By George2 in forum C# Programming
    Replies: 0
    Last Post: 07-16-2007, 08:32 AM
  4. inputFn with hash not assigning pointer correctly
    By mackol in forum C Programming
    Replies: 1
    Last Post: 11-22-2002, 10:17 PM
  5. Why does this not read in correctly?
    By bluebob in forum C Programming
    Replies: 9
    Last Post: 04-19-2002, 09:17 AM

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