Thread: Definition of pointer

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    140

    Definition of pointer

    Hi all

    When a variable V is assigned to a pointer P, then is the address contained in P the address of V at the time of assignment, or is it the subsequent address of V?

    By subsequent I mean that if the address of V is changed (if V is e.g. a pointer), then does P still contain the old address, or the new address?
    Last edited by Niels_M; 10-16-2009 at 08:11 AM.

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    If a pointer is assigned an address (assigned by the value in another pointer) then the 1st pointer holds the address it was assigned at the time of the assignment. If the 2nd pointer's stored address changes, the 1st pointer's stored address will not change.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Code:
    #include <stdio.h>
    
    int main(void) {
    	int x=5, y=10, *xp=&x, *z=xp;
    
    	printf("%d %d\n",*z,*xp);
    	xp = &y;
    	printf("%d %d\n",*z,*xp);
    
    	return 0;
    }
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    Registered User
    Join Date
    Aug 2009
    Posts
    140
    Thanks. Good explanation, and good example.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 03-24-2008, 10:16 AM
  2. Replies: 0
    Last Post: 03-20-2008, 07:59 AM
  3. What is a virtual function pointer?
    By ting in forum C++ Programming
    Replies: 4
    Last Post: 03-05-2008, 02:36 AM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM