Pointer clarification

This is a discussion on Pointer clarification within the C++ Programming forums, part of the General Programming Boards category; i was wondering if someone can clarify something for me. after declaring a pointer eg. int *p; sometimes its assigned ...

  1. #1
    Registered User Spectrum48k's Avatar
    Join Date
    May 2002
    Posts
    66

    Pointer clarification

    i was wondering if someone can clarify something for me.
    after declaring a pointer eg. int *p;
    sometimes its assigned a value like p=x or
    p=&x or *p=x

    my question is this. i understand p=&x assigns the memory
    address of x to p, but when do you use *p=x and
    when p=x ?

    i am doing a lot of exercises mechanically while not fully digesting this, and its bugging me.
    thnx in advance..

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    I'm assuming that 'x' is a regular integer. Like you said, the statement p = &x will assign the pointer p to the address of x. *p = x assigns the value of x to wherever the pointer p points to. In this case that would do nothing because the value of p is already x since that is what it was initialized to. You can't say p = x because p is a pointer and x is an integer. You can do this is x is also a pointer.

    Example:

    Code:
    int main( void )
    {
      int *p = NULL; // Doesn't point to anything yet.
      int y = 5; // Regular variable
      int x = 2; // Regular variable
    
      p = &x; // Now pointing to x
    
      // Output value contained at memory address
      cout << "x = " << x << endl;
      cout << "*p = " << *p << endl;
    
      // Change value at memory address to value of y
      *p = y;
    
      // Now the value of x has changed
      cout << "x = " << x << endl;
      cout << "*p = " << *p << endl;
    
      return 0;
    }

  3. #3
    Registered User
    Join Date
    Feb 2003
    Posts
    595
    my question is this. i understand p=&x assigns the memory
    address of x to p, but when do you use *p=x and
    when p=x ?
    Since p is a pointer, *p = x will copy the value stored in x to the memory location referenced by p. (It would not make sense to do this after doing p = &x since then p would be pointing to x so it would be equivalent to saying x = x.)

    You would NEVER do p = x unless x is also a pointer. Since p is a pointer type, that means it is a variable that can only hold pointer values (memory addresses); you can't give it an int value (or float, or char, etc).

  4. #4
    Registered User Spectrum48k's Avatar
    Join Date
    May 2002
    Posts
    66
    so since p=&x means p is pointing to th memory address
    of x, any value i pass to p will go to the address of x, thus
    manipulating x through its address. is this correct ?
    its alive... its ALIVE... ITS...AL...IIIVE !!!!!!!!!

  5. #5
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,136
    Basically yeah...

    *p=5; will set x equal to 5.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 0
    Last Post: 03-20-2008, 07:59 AM
  2. What is a virtual function pointer?
    By ting in forum C++ Programming
    Replies: 4
    Last Post: 03-05-2008, 01:36 AM
  3. How did you master pointers?
    By Afrinux in forum C Programming
    Replies: 15
    Last Post: 01-17-2006, 07:23 PM
  4. scope of a pointer?
    By Syneris in forum C++ Programming
    Replies: 6
    Last Post: 12-29-2005, 08:40 PM
  5. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 08:32 AM

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