Thread: pointers *shudder*

Hybrid View

Previous Post Previous Post   Next Post Next Post
  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
    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;

  5. #5
    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.

  6. #6
    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;

  7. #7
    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.

  8. #8
    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...

  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
    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.

  11. #11
    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.

  12. #12
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    Quote Originally Posted by quzah
    Actually it generates the address of a given variable.


    Quzah.
    Known in programming circles as a pointer

  13. #13
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Quote Originally Posted by esbo
    Known in programming circles as a pointer
    Really? In my circle it's known as the address of the variable... is the address of my house pointing to the house or is a little piece of paper that contains my address on it pointing to my house? I'm sure at some point the address returned by the address of operator is sitting somewhere in memory, but unless you put it somewhere more permanent, it's not really a pointer. That's just my opinion, I can't speak for other posters on this forum.
    Last edited by SlyMaelstrom; 08-29-2006 at 12:06 PM.
    Sent from my iPadŽ

  14. #14
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Known in programming circles as a pointer
    An address is just an address, but a pointer is a variable capable of holding an address. Saying that an address is a pointer is akin to saying that a bucket is a liquid. Typically, we will blur the difference when talking amongst ourselves, but that's because we really do know the difference and can pull what we need out of the context in which the term is used. However, around here it's best to be as precise as possible to avoid confusion.
    My best code is written with the delete key.

  15. #15
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    Quote Originally Posted by Prelude
    >Known in programming circles as a pointer
    An address is just an address, but a pointer is a variable capable of holding an address. Saying that an address is a pointer is akin to saying that a bucket is a liquid. Typically, we will blur the difference when talking amongst ourselves, but that's because we really do know the difference and can pull what we need out of the context in which the term is used. However, around here it's best to be as precise as possible to avoid confusion.

    "but a pointer is a variable capable of holding an address"

    I wound not agree with that.

    I would say
    "but a pointer variable is a variable capable of holding an address"

    In my opinion a pointer is not necessarilly a variable.

    I mean an interger can be a variable and often is, but it is not always, sometimes it is
    a constant, ie 5.

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