Thread: Lesson 6: An introduction to pointers

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    2

    Lesson 6: An introduction to pointers

    Bleh, I'm taking your tutorial right now, and 1 thing that is killing me before I feel like I know enough to go on is what is the point of pointers?

    I just don't see any reason to using a "pointer" as opposed to just using the variable the pointer is pointing.

    Can someone explain so I don't have to pass this off as "useless" and go on, knowing I probably do need to use it?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Try writing your own strlen() function.
    Then try writing it without using pointers.
    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.

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Idiotofgeniuses View Post
    I just don't see any reason to using a "pointer" as opposed to just using the variable the pointer is pointing.

    Can someone explain so I don't have to pass this off as "useless" and go on, knowing I probably do need to use it?
    Oh boy! The power of C is rooted in pointers! Believe that your attitude will not last.

    It sounds like maybe you have done some programming in a "higher level" or "interpreted" language where pointers aren't used or are concealed as "referencing". But you also may be aware that most of the computer system you are using was programmed in C, and not these other languages! Why? Because it's so efficient.

    The problem is, you cannot use C variables the way you might in (eg) javascript because of pointers. Yes, it makes it slightly more complex to learn to use. But you absolutely have to do it. Believe that there are pointers at work in javascript, because the javascript interpreter is written in C. But you don't have to deal with them.

    Why deal with them in C? Because it has to be dealt with. This also allows YOU to call a lot of shots in ways you wouldn't be able to otherwise. Consider what happens if you have a string:
    Code:
    char string[]="hello world";
    And you want to pass this around between functions, etc. Without pointers, the computer will be stuck copying this string over and over again. That uses more memory, which is an issue in itself. More significantly, it requires a lot more processor activity. This is contrasted with a pointer, which is simply an address of some already existing memory. Much easier, much faster, and requiring much less resources -- except from the programmer. That's why you will never see a commercially $uccessful game (on any platform, including nintendo, etc.) written in anything but C or C++.

    True, javascript, et. al. does not actually do the copying I was talking about. They "manage memory" on a higher lever via. C routines from their source. But these routines must be generalized, whereas in C you do the work yourself.

    You will grow to love them. Start now. A lot of the nitty gritty rationale (and usefulness) will become clear soon. And once that happens, you'll understand how they actually make programming easier.
    Last edited by MK27; 02-21-2009 at 04:44 PM.
    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
    Feb 2009
    Posts
    2
    ok, I think I got it now. You are correct on your theory, as I have worked with python quite a bit and now using "C language" so I can learn more about computer programming.

    I thank you for answering fast, as this place I'm using the internet at has a ever-so-slow connection speed, which is quite frustrating to keep refreshing this while waiting knowing that %50 of my refreshes will say "Server takes too much time to load" and quits (and yes, it is that slow lol).

    I do believe I have a better picture of it now.

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Salem's point is salient to all this -- as far as I know, all C functions are actually written in C, making the language very transparent. Everything is built up from datatypes (including pointers), operators, and semantics.

    Pointers are a necessity, not an esoteric extra tool some people don't really use.
    Last edited by MK27; 02-21-2009 at 05:00 PM.
    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

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. Question about the introduction to pointers lesson
    By Freemind11 in forum C++ Programming
    Replies: 11
    Last Post: 02-15-2006, 11:45 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