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.
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-
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.*
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.
Stage 1.
Woop?
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
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. 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.*
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?Originally Posted by Daved
Knowledge is power and I want it all
-0RealityFusion0-
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.
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-
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
> 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.