Currently Browsing: C/C++

Array implementation by pointers

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
30
31
32
33
34
35
36
37
38
39
40
41
42
//****************************************************************
// We introduce the array implementations by using pointers
//****************************************************************
#include <cstdlib>
#include <iostream>
 
using namespace std;
void printElements(int **&haha,int size1,int size2);
int main(int argc, char *argv[])
{
    //We initalize the arrays' size
    const int size1 = 4,size2 =3;
    // We define a pointer which is ptr
    int *ptr;
    // We initialize out pointer by size of 10
    ptr = new int[10];
    // To show that let'S print the array first element
    ptr[0] = 3;
    cout<<"The first element of the array ptr is "<<ptr[0]<<endl;
    int **pointer;// We implement a matrix
    //Initalization of the columns
    pointer = new int*[size1];
    //Initialization of the rows
    for(int i=0;i<size1;i++)
        pointer[i] = new int[size2];
    //We inialize all the elements to 0
    for(int i=0;i<size1;i++)
        for(int j=0;j<size2;j++)
            pointer[i][j] = 0;
    //We call the method printElements
    printElements(pointer,size1,size2);
    system("PAUSE");
    return EXIT_SUCCESS;
}
void printElements(int **&haha,int size1,int size2){
     for(int i=0;i<size1;i++){
         for(int j=0;j<size2;j++){
             cout<<haha[i][j]; 
         }
         cout<<endl; 
     }
}

A Basic Pointer Example

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;    
}

(more…)

Pass By Referance

//*******************************************************
// We introduce the concept of pass by referance 
// The difference between a method which is 
//implemented by using pass by referance and which is not
//*******************************************************
#include <cstdlib>
#include <iostream>
using namespace std;
//Defining a method which calculates the square of given number
int getSquare1(int num1);
int getSquare2(int &num2);
int main(int argc, char *argv[])
{
    // We define two integer for our two different getSquare method
    int num1 = 2,num2 = 2;
    cout<<"num1 was initially "<<num1<<endl;
    cout<<"num2 was initially "<<num2<<endl;
    // We call the methods
    cout<<"Square of num1 is "<<getSquare1(num1)<<endl;
    cout<<"Square of num2 is "<<getSquare2(num2)<<endl;
    //After calling square methods
    cout<<"num1 is "<<num1<<endl;
    cout<<"num2 is "<<num2<<endl;
    system("PAUSE");
    return EXIT_SUCCESS;
}
int getSquare1(int num1){
    num1 = num1*num1;
    return num1;    
}
int getSquare2(int &num2){
    num2 = num2*num2;
    return num2;    
}
//************************************************************
//The difference between the methods is that the first one do not
//use the variable it creates a variable instead of the num1 but 
//the second method it uses the num2 since we send  adress of the 
//variable to the method so it uses num2 not creates a copy of num2.
//Thus, num2 changes.
//**************************************************************

(more…)

 Sayfa: 4 | Toplam: 4 « 1  2  3  4