CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: Pointers

  1. #1
    Join Date
    Aug 2006
    Location
    Dallas, TX
    Posts
    47

    Pointers

    Hey all,

    I'm new to C++ but not to programming in general. I've knowledge in VB, PHP, and MySQL. But i've never had to deal with pointers before. This is a very new topic to me and it seems irrelevant and pointless. The only benefit i've been able to discern from the use of pointers is to return multiple variables from a function (which can also be done with globals) So can someone please explain why pointers are useful. Any advice on this topic will be appreciated. I've tried reading multiple books on the subject and it just doesn't seem to click.

  2. #2
    Join Date
    Aug 2008
    Posts
    902

    Re: Pointers

    Pointers don't exist for sake of advantage, they are a crucial tool in programming, and the only reason you haven't yet had to work with them is the fact that you've only used extremely high level languages which manage nicely to abstract them away. I don't know what you've written so far, but it's safe to say you can't go far without pointers.

    ...is to return multiple variables from a function (which can also be done with globals)...
    I'm not sure how you came to this understanding, but the use of globals is generally a sign of bad design. They should be avoided whenever possible. I'm not exactly sure how you are using globals in this way, but it sounds like it is the worst violation of the use of globals (i.e. everywhere.)

  3. #3
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    Re: Pointers

    If you use globals to communicate results from a function, it is hard to apply that function to different input data, because you would need to copy the results from the global to a local variable in order to retain the value (the next time the function gets called, it will be overwritten). And even that depends heavily on a single threaded design. Once you introduce multiple threads, you're completely lost with such a design.

    Besides, when function arguments are pointers, you should always consider if a reference is not more appropriate. The difference (in the context of function arguments) is that a pointer can be NULL, whereas a reference must always refer to a valid object. If it doesn't make sense for the pointer to be NULL, you should use a reference and not a pointer.

    The use of pointers will probably become clearer when you learn how to use classes. Some constructs, such as a linked list data structure, cannot be implemented in C++ without using pointers. Also, pointers come in real handy when you want to use classes polymorphically.
    When you just consider functional programming, the main use of pointers is for optional (output) arguments of a function.
    Code:
    void ComputeDivAndMod(int x, int y, int* div, int* mod)
    {
        if (div) {
            *div = x / y;
        }
        if (mod) {
            *mod = x % y;
        }
    }
    
    int main()
    {
        int a = 10, b = 3, c, d;
        // if we only want div
        ComputeDivAndMod(a, b, &c, NULL);
        // if we only want mod
        ComputeDivAndMod(a, b, NULL, &d);
    }
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

  4. #4
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Pointers

    Quote Originally Posted by cypher5783 View Post
    So can someone please explain why pointers are useful.
    Code:
    void ChangeToZero( ??? )
    {
        //???
    }
    
    int main()
    {
       int x = 10;
       int y = 20;
       int z = 30;
       ChangeToZero( ??? );
    }
    Write a single function ChangeToZero that can take either x, y, or z, or any integer variable I desire, and change that variable's value to 0. Remember, you can only write one function to do this, and the function knows nothing about the x, y, and z that I've declared local to main(). Once you've solved that problem, then you know pointers.

    Secondly, have you ever used scanf()? If so, what are the arguments you provide to get the input? Pointers. How about strcpy(), strlen(), strcat(), memcpy() -- ever used those functions? Again, what arguments must you provide? Pointers. Why do you think these functions require pointers as arguments?

    Last, polymorphism, the thing that makes C++ what it is, requires usage of pointers and/or references to have this paradigm work. If you don't understand pointers, then you don't understand polymorphism.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; April 8th, 2011 at 05:35 AM.

  5. #5
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,656

    Re: Pointers

    Imagine that I have a great book with answers to all you questions. In a world without pointers, I would have to copy/paste it here for you, but if we DO have pointers, I would simply point you to this book
    Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
    Convenience and productivity tools for Microsoft Visual Studio:
    FeinWindows - replacement windows manager for Visual Studio, and more...

  6. #6
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: Pointers

    In c++ you can't take advantage of polymorphism without pointers.

    When you allocate memory on the heap, the OS returns an address to the memory it's allocated. It's the only way you can access that memory.

    Many data structures use pointers to connect related elements. A linked list is a good example of this.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured