Thread: Why would I need pointers.

  1. #1
    Registered User
    Join Date
    Dec 2001
    Posts
    18

    Question Why would I need pointers.

    I can't say I'm a newbie, but I can't say I'm a complete n00b either .

    My question is this:
    Why would you need pointers? I know how they work, and I know how to use them. What I don't know is why should I use them.

    Just curious.

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145

    dynamics

    Pointers are great if you want to create an array with a number of elements that is not constant (if the user enters a number, an array with so many elements will be created). You can't do something like this:

    int Array[NrOfElements];

    Do this instead:

    int* Array;
    Array=new int[NrOfElements];
    delete[] Array;

    Pointers have lots of other uses too, this is only one of them.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Registered User
    Join Date
    Dec 2001
    Posts
    18
    I didn't know about that . Any more ways to use pointers?

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Put simply and without having to describe every situation, pointers give C and C++ their power. Using pointers you can write code that is much more efficient in both speed and memory usage, easier to use in the case of abstract data types such as trees and linked lists and more compact in relation to similar code in other languages.

    -Prelude
    My best code is written with the delete key.

  5. #5
    Registered User
    Join Date
    Dec 2001
    Posts
    18
    I'll pretend I understood that. I will have to, some day. Not any time soon, though.

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    All in good time, you'll begin to understand the more you use them. For now I'm afraid it's just going to be one of those things that you have to do now and trust that you'll learn later

    -Prelude
    My best code is written with the delete key.

  7. #7
    Registered User
    Join Date
    Dec 2001
    Posts
    20
    Go back and take another look at the array reply and its use of new. New returns a pointer, so that's a good reason for needing to know how to deal with them.

    So why do you need New. Because C/C++ does not let you specify at RUNTIME how big an array you want, unless you use use "new". Try it: the compiler will say that size has to be a constant which means you have to enter it at compile time, not runtime.

    OK you say, I don't need runtime declared arrays. Well actually, all significant memory structures you want to declare at runtime require new. It's called dynamic memory allocation.

    BTW, I'm new at this, so if some guru wants to correct or clarify, I'll be happy to learn from that too.
    Regards, Al

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    So far so good Al.

    -Prelude
    My best code is written with the delete key.

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