Results 1 to 8 of 8

Thread: C++ pointers

  1. #1
    Join Date
    Jun 2012
    Beans
    10

    C++ pointers

    hi,
    i am starting to learn about pointers. Exactly what is the purpose of using them? when you can simply declare the variables instead?

  2. #2
    Join Date
    Sep 2009
    Location
    Canada, Montreal QC
    Beans
    1,809
    Distro
    Ubuntu 11.10 Oneiric Ocelot

    Re: C++ pointers

    This topic should be covered by your tutorial or book. I highly recommend The C++ Programming language written by its inventor.
    I know not with what weapons World War III will be fought, but World War IV will be fought with sticks and stones.
    Freedom is measured in Stallmans.
    Projects: gEcrit

  3. #3
    Join Date
    Aug 2006
    Location
    60°27'48"N 24°48'18"E
    Beans
    3,458

    Re: C++ pointers

    Pointers are essentially an indirection abstraction. When you can build things of references to things -- or of references to things with further references -- you can structure arbitrarily nested things at runtime. Try writing a non-trivial data structure and you'll quickly figure out why you'll want an ability to refer to a thing by not using the thing in itself.
    LambdaGrok. | #ubuntu-programming on FreeNode

  4. #4
    Join Date
    Jul 2012
    Beans
    22

    Re: C++ pointers

    A few years ago I asked the same thing.

    Pointers aren't very useful in very small programs (the type used to illustrate concepts of C++ or any language) so declaring variables is easier.

    To illustrate the point of pointers (puns, puns, puns everywhere), consider this code

    Code:
    #include <iostream>
    using namespace std;
    void triple(int * number); //function prototype
    
    int main()
    {
        int num = 5;
        int *ptr = &num; //pointer to num location
        
        cout<< "num value is "<<num<<endl;
        triple(ptr);      //pass num location to function
        cout<< "num value is now "<<num<<endl;
        int pause;
        cin>>pause;
        return 0;
    }
    
    void triple (int * number)
    {
         *number = *number * 3; //change the value of num
    }
    (First off this code isn't mine, it's from a bookmark I had when I was learning about them (http://www.cplusplus.com/forum/beginner/337/)) The program creates a variable (int num = 5) and pointer to that variable in the next line. The pointer is passed to the function triple. Since the pointer is pointing to the address of num, it's like passing num to triple.

    This can be done without the pointers but it does illustrate their point (pun)

    Also a function cannot return char but it can return char* (pointer to that char variable) I believe (I'm not sure about that, I have to brush up on my C++, and I was quite amateur to begin)

  5. #5
    Join Date
    May 2007
    Beans
    251

    Re: C++ pointers

    Quote Originally Posted by Lymphocyte View Post
    hi,
    i am starting to learn about pointers. Exactly what is the purpose of using them? when you can simply declare the variables instead?
    The simplest definition of pointers is they are like normal variables but with the sole purpose of storing addresses of other variables.

    Like CptPicard mentioned, they're essentially an abstraction of indirection - which allows for some really nifty stuff - like building really complex data structures. Since pointers are like regular variables, their contents (essentially, an address) can be changed/modified at runtime. That is, they can be made to point to different objects in memory at runtime - which is not possible using just references.
    The Unforgiven

  6. #6
    Join Date
    Oct 2011
    Location
    Chicago, IL
    Beans
    419
    Distro
    Xubuntu 10.04 Lucid Lynx

    Re: C++ pointers

    Quote Originally Posted by SirWhy View Post
    Pointers aren't very useful in very small programs (the type used to illustrate concepts of C++ or any language) so declaring variables is easier.

    To illustrate the point of pointers (puns, puns, puns everywhere), consider this code

    Code:
    #include <iostream>
    using namespace std;
    void triple(int * number); //function prototype
    
    int main()
    {
        int num = 5;
        int *ptr = &num; //pointer to num location
        
        cout<< "num value is "<<num<<endl;
        triple(ptr);      //pass num location to function
        cout<< "num value is now "<<num<<endl;
        int pause;
        cin>>pause;
        return 0;
    }
    
    void triple (int * number)
    {
         *number = *number * 3; //change the value of num
    }
    ...
    The preferred way to do something like that in C++ is by reference.

    Quote Originally Posted by SirWhy View Post
    Also a function cannot return char but it can return char* (pointer to that char variable) I believe (I'm not sure about that, I have to brush up on my C++, and I was quite amateur to begin)
    This is incorrect. Could you perhaps be confused about trying to return a string literal instead of a dynamically allocated char*?


    In the C++ development I've done, the general mindset is that you should avoid the use of raw pointers at any reasonable cost. Preferred to raw pointers are references, STL containers and 'smart pointers' in the boost library (I think that the smart pointers are going to be standardized as well)

    To answer your question, a more relevant example of where pointers are useful might be a linked list. Each node of the list has some kind of useful data and a pointer to the next node in the list. Suppose for the sake of argument that the useful data in each element in the list is a 32-bit integer and a 64-bit double.

    Without the use of pointers, the last item in the list would be 96 bits, the one before that would be 192 bits, and the item before that would be 288...384...480...and so forth

    Instead of actually embedding the next node into its parent, the practical way to solve this problem would be to store the next node as a Node*. That way every item in this (admittedly contrived) list would be 128 bits (using 32-bit addresses)

  7. #7
    Join Date
    Jun 2007
    Location
    Maryland, US
    Beans
    6,288
    Distro
    Kubuntu

    Re: C++ pointers

    Quote Originally Posted by 11jmb View Post
    Could you perhaps be confused about trying to return a string literal instead of a dynamically allocated char*?
    Returning a string-literal is permitted too. For example:
    Code:
    const char* helloworld()
    {
        return "Hello World!";
    }
    As you pointed out (no pun), using pointers is generally considered the last option to consider in C++, however when allocating memory on the heap, they are essential. In the case of char pointers, a better option to consider is using the STL string class (which encapsulates a char pointer).

  8. #8
    Join Date
    Jul 2012
    Beans
    22

    Re: C++ pointers

    This is incorrect. Could you perhaps be confused about trying to return a string literal instead of a dynamically allocated char*?
    Exactly, as I said I too need to brush up on my C++ knowledge

Bookmarks

Posting Permissions

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