Thread: What is the point of pointers?

  1. #1
    Novice.
    Join Date
    Oct 2005
    Posts
    88

    What is the point of pointers?

    What is the point of pointers? Allright, you can store values in RAM with it. So can you with:
    Code:
    int a = 3;
    So what's the point of using pointers?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I suggest that you read the tutorial.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Novice.
    Join Date
    Oct 2005
    Posts
    88
    Allright maybe I should have stated my question more clearly. I've read the tutorial, a couple of times. I've used pointers, but still haven't found an advantage in them above the normal integers I use for my (simple) programs.

    The only really useful point I see is that you can get an amount of memory for a certain junk of data of which you don't know the size beforehand another is: (I cite) "If you happen to have a huge piece of data that you want to pass into a function, it's a lot easier to pass its location to the function than to copy every element of the data!" I don't really understand this since I just use the variable. I bet all of this is really useful if you're good at C, but it's a beginner's tutorial so what's the use of it for me?
    Last edited by omnificient; 12-14-2007 at 11:22 AM.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Try implementing something trivial like strcpy() 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.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Doing like you do stores memory on the stack which is limited, usually to around 1 MB (and even less in embedded systems!).
    Also, all variables allocated on the stack disappears when the function exits!
    Further, if you pass your data to other functions without pointers, a copy is created! So the function you call will be able to modify its own copy of the data, thus the changes are not reflected in the function (caller) that called the new function (callee).
    If you want the callee to be able to manipulate or change the memory you passed, you need to do so using pointers.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    No, you're right, pointers are completely useless - no need to learn about them....

    Of course, if that was the case, why the the jokers that invented the language include them, and why are people using them all the time?

    Perhaps they are useful, after all?

    Actually, I can make strcpy() without (apparent) pointers;
    Code:
    void strcpy(char dest[], char src[])
    {
       int i;
       for(i = 0; dest[i] = src[i]; i++) ;
    }
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    But that is basically pointers, is it not? Though you may say the function takes arrays, in reality the compiler will treat them as pointers and pass the address. And the values also inhibit the properties of pointers if you try to look at their contents (you can only see the first index, though the caller can see the entire).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Novice.
    Join Date
    Oct 2005
    Posts
    88
    Ah thanks, now it's clear!

  9. #9
    Registered User
    Join Date
    Dec 2007
    Posts
    16
    It's like the difference between shipping a house to someone so that they can see it, or sending them the house address so they can go see it where it's at. Which is more efficient?

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Yes, but of course, since you ship a house, it's not THE house - it's just similar so you can change it but the real house won't see a difference.
    Of course, this applies to really big objects. If it's just a lamp, the difference is less subtle, but there.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Just stick something like "why use pointers" into google . . . .
    http://duramecho.com/ComputerInforma...CPointers.html
    http://www.sparknotes.com/cs/pointer...s/summary.html
    http://www.cs.cf.ac.uk/Dave/C/node10.html

    Without pointers, you can't use dynamic memory allocation. Enough said.
    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.

  12. #12
    Madly in anger with you
    Join Date
    Nov 2005
    Posts
    211
    Quote Originally Posted by matsp View Post
    No, you're right, pointers are completely useless - no need to learn about them....

    Of course, if that was the case, why the the jokers that invented the language include them, and why are people using them all the time?

    Perhaps they are useful, after all?

    Actually, I can make strcpy() without (apparent) pointers;
    Code:
    void strcpy(char dest[], char src[])
    {
       int i;
       for(i = 0; dest[i] = src[i]; i++) ;
    }
    --
    Mats
    that's not strcpy(), at least, not how ANSI defines it. :P

    char * strcpy ( char * destination, const char * source );
    Return Value
    destination is returned.

  13. #13
    Beautiful to C Aia's Avatar
    Join Date
    Jun 2007
    Posts
    124
    Quote Originally Posted by sl34k View Post
    that's not strcpy(), at least, not how ANSI defines it. :P
    Missing the point...I mean the pointer;...no, no the point?

    Quote Originally Posted by omnificient View Post
    What is the point of pointers? ....

    So what's the point of using pointers?
    What's the point again?...where's the point?.
    I like points. I collect points. C'mon, pass me
    the point. I live for points. Life is full of points.
    ...I see...dead points.

  14. #14
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Not a resurrection of the "pointless" thread . . . you know, the one that got locked because everyone was cracking point jokes?
    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
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    And this one is not far from the same fate.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Drawing Program
    By Max_Payne in forum C++ Programming
    Replies: 21
    Last Post: 12-21-2007, 05:34 PM
  2. trouble with overloaded operator
    By kkurz in forum C++ Programming
    Replies: 2
    Last Post: 10-31-2003, 12:59 PM
  3. do pointers affect what they point to directly?
    By PHP in forum C++ Programming
    Replies: 6
    Last Post: 01-14-2003, 01:29 PM
  4. Staticly Bound Member Function Pointers
    By Polymorphic OOP in forum C++ Programming
    Replies: 29
    Last Post: 11-28-2002, 01:18 PM
  5. point lies in circle?
    By cozman in forum Game Programming
    Replies: 3
    Last Post: 12-20-2001, 04:39 PM