What is the point of pointers?

This is a discussion on What is the point of pointers? within the C Programming forums, part of the General Programming Boards category; What is the point of pointers? Allright, you can store values in RAM with it. So can you with: Code: ...

  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
    21,630
    I suggest that you read the tutorial.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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 10:22 AM.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,494
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,543
    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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,543
    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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,543
    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,046
    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
    Aia
    Aia is offline
    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,046
    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
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    And this one is not far from the same fate.

Page 1 of 2 12 LastLast
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, 04:34 PM
  2. trouble with overloaded operator
    By kkurz in forum C++ Programming
    Replies: 2
    Last Post: 10-31-2003, 11:59 AM
  3. do pointers affect what they point to directly?
    By PHP in forum C++ Programming
    Replies: 6
    Last Post: 01-14-2003, 12:29 PM
  4. Staticly Bound Member Function Pointers
    By Polymorphic OOP in forum C++ Programming
    Replies: 29
    Last Post: 11-28-2002, 12:18 PM
  5. point lies in circle?
    By cozman in forum Game Programming
    Replies: 3
    Last Post: 12-20-2001, 03:39 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21