1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | //***************************************************************** // In this example we introcuce pointer concept. It does not //explain everything but it is rather an introduction //***************************************************************** #include <cstdlib> #include <iostream> using namespace std; // A method by using concept of pointer int square(int *haha); int main(int argc, char *argv[]) { // Simply pointers int num1 = 5; int *num2;//num2 is a pointer decleration //It is not a number but it is an adress of number //As the name implies it points the number which we want num2 = &num1;//num2 holds an adress thus we give the num1's // adress to the num2 cout<<"num1 is "<<*num2<<endl; cout<<"If use it in a method "<<endl; cout<<"The square of num1 is "<<square(&num1)<<endl; cout<<"The num1 is "<<num1<<endl; system("PAUSE"); return EXIT_SUCCESS; } int square(int *haha){ *haha = *haha * *haha; return *haha; } |
Output:
