Thread: pointers *shudder*

  1. #1
    Learner Axel's Avatar
    Join Date
    Aug 2005
    Posts
    335

    pointers *shudder*

    I believe i have a fair understanding of pointers. What i don't understand or rather if i'm under estimating the powerfulness of pointers. So basically we can by-pass call by value restrictions, they can be used as part of linked lists but what else?

    I have come to conclusion that pointers offer the following benefits?

    - We can dynamically modify whatever the pointer is pointing to. So for example we could have a method which does something and modify the contents of the pointed value, we can then print the value in the main method by de-referencing the pointer and finding out what its pointing *TO* (i.e. *xPtr = 2) makes x =2 if *xPtr = &x (if *xPtr points to X), rather than using global variables (which are bad).

    - They're the equivalent of getters and setters in java (or kind of??)

    And i've also written something small to demonstrate my basic understanding. Please correct me if i'm wrong:

    Code:
    #include <stdio.h>
    void calculate(int *xPtr);
    int main()
    {
    	int x = 0;
    	int *xPtr; // Create a pointer and a memory location is allocated.
    	
    	xPtr = &x;
    	*xPtr = 2;
    	//calculate(&x); // Pass a reference to the variable x, which will make xPtr point to x
    	
    	printf("%d", x); // print what x contains
    	printf("%d", *xPtr); // print what xPtr is pointing to in this case x.
    	
    	return 0;
    }
    
    void calculate(int *xPtr)
    {
    	*xPtr = 2; //set whatever xPtr is pointing to (x) to 2.
    }
    Also what stumps me is the following:

    If i pass a reference of x to calculate it makes xptr point to X and sets xptr to 2 which makes X 2 since xptr is pointing to x now. But for some reason i can't de-reference *xPtr and print it the program just crashes ?

    Code:
    #include <stdio.h>
    void calculate(int *xPtr);
    int main()
    {
    	int x = 0;
    	int *xPtr; // Create a pointer and a memory location is allocated.
    	
    	//xPtr = &x;
    	calculate(&x); // Pass a reference to the variable x, which will make xPtr point to x
    	
    	printf("%d", x); // print what x contains
    	printf("%d", *xPtr); // print what xPtr is pointing to in this case x.
    	
    	return 0;
    }
    
    void calculate(int *xPtr)
    {
    	*xPtr = 2; //set whatever xPtr is pointing to (x) to 2.
    }
    Last edited by Axel; 08-28-2006 at 06:55 AM.

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    You forgot about dynamic memory allocation, which is probably the most important function of the pointer. This is why your second example is broken.
    Last edited by SlyMaelstrom; 08-28-2006 at 06:59 AM.
    Sent from my iPadŽ

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >But for some reason i can't de-reference *xPtr and print it the program just crashes ?
    You're trying to dereference an uninitialized pointer. Before you can dereference a pointer, it must point to memory that you own, either by assigning the address of an existing object or allocating dynamic memory.
    My best code is written with the delete key.

  4. #4
    Registered User Rennor's Avatar
    Join Date
    Aug 2006
    Location
    Finland
    Posts
    45
    I am thinking about your compilers warning level, it should have warned you about using uninitialized variable.

  5. #5
    Learner Axel's Avatar
    Join Date
    Aug 2005
    Posts
    335
    Quote Originally Posted by Prelude
    either by assigning the address of an existing object or allocating dynamic memory.
    x is an exisiting object i'm also assigning it &x and pass it to calculate which accepts a pointer.

    Isn't it the same as :

    *xPtr = &x;

  6. #6
    Learner Axel's Avatar
    Join Date
    Aug 2005
    Posts
    335
    so:

    Code:
    #include <stdio.h>
    void calculate(int *xPtr);
    int main()
    {
    	int x = 0;
    	int *xPtr = &x; // Make xPtr point to x.
    	
    	calculate(&x); // Pass a reference to the variable x, which will make xPtr point to x??? OR
    	
    	printf("%d", x); // print what x contains
    	printf("%d", *xPtr); // print what x contains
    	
    	return 0;
    }
    
    void calculate(int *xPtr)
    {
    	*xPtr = 2; //set whatever xPtr is pointing to (x) to 2.
    }
    it still defeats the purpose of calculate accepting a memory reference...

  7. #7
    Registered User Rennor's Avatar
    Join Date
    Aug 2006
    Location
    Finland
    Posts
    45
    Quote Originally Posted by Axel
    Isn't it the same as :

    *xPtr = &x;
    That would be all wrong since *xPtr while being uninitialized is pointing "nowhere" or "somewhere" And you try to assign it value which is reference of x.

  8. #8
    Learner Axel's Avatar
    Join Date
    Aug 2005
    Posts
    335
    Quote Originally Posted by Rennor
    That would be all wrong since *xPtr while being uninitialized is pointing "nowhere" or "somewhere" And you try to assign it value which is reference of x.

    sorry i meant xPtr = &x;

  9. #9
    Learner Axel's Avatar
    Join Date
    Aug 2005
    Posts
    335
    ok i'm guessing:

    Code:
    void calculate(int *xPtr)
    {
    	*xPtr = 2; //set whatever xPtr is pointing to (x) to 2.
    }
    this only sets whatever xPtr is pointing to before this it's not pointing to no (i.e unalloacted reference) where so therefore is the reason why it crashed.

  10. #10
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >sorry i meant xPtr = &x;
    You failed to do that in the code that had the problem. To be more specific, you commented it out:
    Code:
    int *xPtr;
    	
    //xPtr = &x;
    calculate(&x); /* xPtr is still uninitialized here */
    My best code is written with the delete key.

  11. #11
    Learner Axel's Avatar
    Join Date
    Aug 2005
    Posts
    335
    what confuses me is that:

    void calculate(int *xPtr)


    i'm passing an &x to it. When the arguments accept a pointer (i.e. xPtr is pointing to x) Why is that?

  12. #12
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Quote Originally Posted by Axel
    what confuses me is that:

    void calculate(int *xPtr)


    i'm passing an &x to it. When the arguments accept a pointer (i.e. xPtr is pointing to x) Why is that?
    Huh? I don't understand what your question is.
    If you understand what you're doing, you're not learning anything.

  13. #13
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Quote Originally Posted by Axel
    what confuses me is that:

    void calculate(int *xPtr)


    i'm passing an &x to it. When the arguments accept a pointer (i.e. xPtr is pointing to x) Why is that?
    xPtr is a pointer which accepts the address of an integer. x is an integer, so you pass the address of x (&x) to the pointer.
    Sent from my iPadŽ

  14. #14
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    &x generates a pointer, a pointer to x.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  15. #15
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Actually it generates the address of a given variable.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using pointers to pointers
    By steve1_rm in forum C Programming
    Replies: 18
    Last Post: 05-29-2008, 05:59 AM
  2. function pointers
    By benhaldor in forum C Programming
    Replies: 4
    Last Post: 08-19-2007, 10:56 AM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  4. Staticly Bound Member Function Pointers
    By Polymorphic OOP in forum C++ Programming
    Replies: 29
    Last Post: 11-28-2002, 01:18 PM