Question about pointers.

This is a discussion on Question about pointers. within the C++ Programming forums, part of the General Programming Boards category; Would there be any purpose in using a pointer in program that only contains main(). The way I understand pointers, ...

  1. #1
    1479
    Join Date
    Aug 2003
    Posts
    253

    Question about pointers.

    Would there be any purpose in using a pointer in program that only contains main(). The way I understand pointers, I can't see a practical use for doing this.
    Knowledge is power and I want it all

    -0RealityFusion0-

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Dynamically allocated memory?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    If you needed to allocate a huge array (and didn't want to use a vector or std::tr1::array for some reason), so you use the heap instead of a local array.

    If you had two derived classes (that may violate your main-only requirement) and wanted to instantiate one or the other depending on some variable, you would use a Base class pointer and use new in an if/else block. You wouldn't use a reference in that case since it would go out of scope if you initialized it in the if/else block.

  4. #4
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,812
    Stage 1.
    Woop?

  5. #5
    Registered User
    Join Date
    Jun 2002
    Posts
    230
    well realistically speaking there really isnt much use for a program that only contains just main. yea you can make a calculator but to make really useful programs your going to need classes for different things and thats really where the heart of c++ and pointers are. im pretty sure an done anythin you do with just main can be done without pointers.
    C++ Rules!!!!
    ------------
    Microsoft Visual Studio .NET Enterprise

  6. #6
    1479
    Join Date
    Aug 2003
    Posts
    253
    Ok I think I am finally getting this ironed out. After a few hours of toying with this I came up with some code. Would this code be a representation of a good way to use pointers?
    Code:
    #include <iostream>
    
    using namespace std;
    
    int testchange(int *, int *);
    
    int main()
    {
        int x;
        int y;
        static int two;
        
        cout <<"Enter a value" <<endl;
        cin >>x;
        cout <<"Enter a value" <<endl;
        cin >>y;
        cout <<"Value of x before the function call " <<x <<endl;
       testchange(&x, &y);
       cout <<"==============================" <<endl;
       cout <<"Values back in main!" <<endl;
       cout <<"==============================" <<endl;
       cout <<"Value of x in main after the function call " <<x <<endl;
       cout <<"Back in main " <<y <<endl;
        
        system("PAUSE");
        return 0;
    }
    int testchange(int *a, int *b)
    {
        
        cout <<"================================" <<endl;
        cout <<" THE FOLLOWING ARE MEMORY LOCATIONS OF THE VALUES " <<endl;
        cout <<endl;
        cout <<"Location of the value of variable a in function " <<a <<endl;
        *a = *a + 1;
        cout <<"Location of the value of variable b in function " <<b <<endl;
        cout <<"==================================" <<endl;
        cout <<"++++++++++++++++++++++++++++++++++" <<endl;
        cout <<"Actual value of a " <<*a <<endl;
        cout <<endl;     
    }
    Knowledge is power and I want it all

    -0RealityFusion0-

  7. #7
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    I would use references for cases like that.

  9. #9
    1479
    Join Date
    Aug 2003
    Posts
    253
    Quote Originally Posted by Daved
    I would use references for cases like that.
    So would it be worth while to keep this bit of code for me to refresh my memory about how pointers work? Or is it more geared for using references?
    Knowledge is power and I want it all

    -0RealityFusion0-

  10. #10
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    It's a fine example. It shows the idea of pass-by-reference, which can be done with pointers or references. It shows how to use the address-of operator to get a pointer from a regular variable. It shows how to dereference a pointer. Of course, tutorials like the one linked by Dave_Sinkula also have examples of that.

  11. #11
    1479
    Join Date
    Aug 2003
    Posts
    253
    Dang, I was hoping it was more of a straight up way to use pointers. Instead it seems that my code show how to pass by reference...guess I have a lot more reading to do.
    Knowledge is power and I want it all

    -0RealityFusion0-

  12. #12
    Registered User
    Join Date
    Jun 2002
    Posts
    230
    Pointers are used more for association between things. For example if i had a class Class spouse and i wanted to somehow marry different people. I would use pointers to link the husband and wife together.
    C++ Rules!!!!
    ------------
    Microsoft Visual Studio .NET Enterprise

  13. #13
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,821
    > Would there be any purpose in using a pointer in program that only contains main()
    int main ( int argc, char *argv[] )
    There's your pointer - what's the question?
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array pointers question?
    By ben2000 in forum C Programming
    Replies: 4
    Last Post: 07-26-2007, 02:31 AM
  2. A question on Pointers & Structs
    By FJ8II in forum C++ Programming
    Replies: 4
    Last Post: 05-28-2007, 11:56 PM
  3. simple pointers question
    By euphie in forum C Programming
    Replies: 4
    Last Post: 05-25-2006, 02:51 AM
  4. Very stupid question involving pointers
    By 7smurfs in forum C Programming
    Replies: 6
    Last Post: 03-21-2005, 06:15 PM
  5. Quick Question Regarding Pointers
    By charash in forum C++ Programming
    Replies: 4
    Last Post: 05-04-2002, 12:04 PM

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