Thread: Getting a grasp on pointers

  1. #1
    Registered User
    Join Date
    Jul 2006
    Posts
    111

    Getting a grasp on pointers

    Hello all, I am quite new to C, and Iam trying to learn pointers. I know how to declare them and what not, I am just confused on their uses. Does anyone know of any examples that might give me a better understanding? I read the article of pointers on this site and it was of some help, but I am still a little confused.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >but I am still a little confused
    About what? Be specific and we can help you more effectively.
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Jul 2006
    Posts
    111
    I am confused why they are used. I can think of some applications but not many.
    Last edited by lilrayray; 07-25-2006 at 10:03 AM.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >I am confused
    Yes, we got that part.

    >why they are used
    Because they solve problems that require indirection. Your question is no more specific, and because pointers are a very broad subject, it's extremely difficult to give you a good answer without writing a book.

    >I can think of some applications but not many.
    My usual recommendation is not to worry about it. You presumably know what pointers are and how to use them. As you progress in learning how to program, you'll find more and more places where you can apply that knowledge.

    A certain measure of experience is necessary to understand why certain features are useful. If you don't have the experience to match a feature with a need, you won't understand why the feature is needed. As an example, most people don't understand why functions are necessary when they first start. Then after they have a long main function with lots of duplicated code, they gain experience in the problem domain that introduces the need for functions. Suddenly things click and they understood why we need and use functions.
    My best code is written with the delete key.

  5. #5
    Registered User
    Join Date
    Jul 2006
    Posts
    111
    Ok. So just write programs with my current knowledge and eventually I will figure out its uses? Sounds easy enough.

  6. #6
    Registered User
    Join Date
    May 2006
    Posts
    57
    One of the important uses of variables is in functions. For function arguements, you can pass variables by value, or by reference.

    Pass by value means you pass to the function a copy of the value of the variable in question, you can't change the value of the actual variable.

    If you pass a pointer to a variable to a function, that is passing by reference, and you can directly change the value of the actual variable in question from the function.

  7. #7
    Registered User
    Join Date
    Apr 2006
    Posts
    13
    I am fairly new to C as well. Although I now can understand why pointers are used etc etc. I find that a lot of the books have a chapter called "Poiters" but they generally really never touch on what the point of them is. I found K and R makes the use of pointers clearer than most texts, its worth a look at!!!!

    just my thoughts

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Quote Originally Posted by MDofRockyView
    Pointers are part of the language definition because of the way that the compiler allocates memory for data storage. There is something called a stack, and something called a heap, and function varables are either allocated memory inside the function stack (which is a structure type) or else from the a pool of free memory (heap memory..which is where all memory eventually returns). When you invoke a function a new stack is created, and the old stack released into the heap, and the variables in this new function are independant of the caller (the function you just left) so if you pass arguments by value, copies of the variables are defined in the new stack. A special type of variable, a pointer, is a variable that is defined just like any argument on the new stack, but it has a special property which allows it to contain the memory address of the variable in the heap, and another property that allows you to change the value stored in that variable through a method known as indirection.
    Fascinating. I guess this is what it looks like when someone with half a clue tries to fill in the blanks. By the way, you never addressed the original question.

    >I guess that Prelude has little time for imagination, because being specific requires some general knowledge.
    No, I'm just enough of a perfectionist to try listing every possible use. There's a pointer tutorial on my website, but it could probably do with a rewrite, which is why I didn't link it.
    My best code is written with the delete key.

  9. #9
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Quote Originally Posted by MDofRockyView
    I guess that Prelude has little time for imagination, because being specific requires some general knowledge.
    Prelude has probably not the time to explain every possible use of a pointer. More, she feels it will be more beneficial to the OP to understand the "why" of pointers if, instead of giving him a crude example, she allows him to find it by himself as he continues to study the language... because, you get it, other language features use pointers. I agree with her on both accounts.

    Quote Originally Posted by MDofRockyView
    Pointers are part of the language definition because of the way that the compiler allocates memory for data storage.
    You couldn't be more wrong.

    Quote Originally Posted by MDofRockyView
    There is something called a stack, and something called a heap, and function varables are either allocated memory inside the function stack (which is a structure type) or else from the a pool of free memory (heap memory..which is where all memory eventually returns).
    This has nothing to do with pointers and you are confusing the OP.

    Quote Originally Posted by MDofRockyView
    When you invoke a function a new stack is created, and the old stack released into the heap
    Wrong.

    Then you have some more rambling followed by an outstanding:
    Quote Originally Posted by MDofRockyView
    I imagine that is how it works anyway.
    So... at the light of that last statement, why did you post in the first place? Do you think you provided a good service explaining the OP the use of pointers?

    Prelude, with less time in her hands, did a better service than your well-intentioned but outright wrong Red Cross style attempt.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  10. #10
    Registered User Osaou's Avatar
    Join Date
    Nov 2004
    Location
    Stockholm, Sweden
    Posts
    69
    So... your reply, MD, was simply a disguised question on how memory allocation is handled by the compiler and what happens when a function is called. Sneaky.
    Last edited by Osaou; 07-26-2006 at 01:02 PM.

  11. #11
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Quote Originally Posted by MDofRockyView
    The only way that people with no imagination can be correct is if they narrow something down until it is trivial. Go ahead and prove me wrong, if you know all of the answers. What good does "wrong" suggest, if it is not supported by the correct answer? Please enlighten me with your knowledge...
    Why should I? This isn't even the objective of this thread.

    You are the one that on a pathetic attempt to look smart ended up looking... not smart, to put it mildly.

    I suggest you crawl out silently. It will serve you better
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  12. #12
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >So you are suggesting that I accept that my answers are wrong for no reason?
    He's suggesting that you stop hijacking this thread and make a new one if you have an unrelated question.
    My best code is written with the delete key.

  13. #13
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    We have a troll. Can someone please close this thread?
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  14. #14
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > Does anyone know of any examples that might give me a better understanding?
    Well you could try writing strlen() without using pointers.

    Two common reasons
    1. You need to do the same kind of thing on different data sets (like for example working out the length of different strings).
    2. You need to allocate space at run time, and you get to the allocated memory via a pointer.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  15. #15
    Registered User
    Join Date
    Jul 2006
    Posts
    111
    @ Prelude it seems like when you answer a thread, trouble starts.

    @Salem - Thanks for the reply. I am starting to understand it better now.

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. Replies: 4
    Last Post: 12-10-2006, 07:08 PM
  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