beginner in classes. pointers problem

beginner with classes. The following code didn't run with pointers. but ran fine without pointers or even with reference. error says:
This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.

With pointers(didn't run)
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
43
44
///first few code for background
  class vectorofint
{
    public:
    vectorofint();
    void push_front(int value, int *ini_size);
    
    private:
    int *array;
};

vectorofint::vectorofint()///constructor with no arguement

{
    array=new int[32];

}

///the code with pointer start here
void vectorofint::push_front(int value, int *ini_size)///problem in *ini_size 
{
    int *temp=new int[*ini_size];
    for(int i=0;i<*ini_size;i++)
    {
        temp[i]=array[i];
    }
    delete array;
    *ini_size++;
    array=new int[*ini_size];
    array[0]=value;
    for(int i=1;i<(*ini_size);i++)
    {
        array[i]=temp[i-1];
    }
    delete temp;
}


int main()
{
   int ini_size=22;
    vectorofint a();
    a.push_front(34, &ini_size);
}


the following code runs fine:(without ptr)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
void vectorofint::push_front(int value, int ini_size)
{
    int *temp=new int[ini_size];
    for(int i=0;i<ini_size;i++)
    {
        temp[i]=array[i];
    }
    delete array;
    ini_size++;
    array=new int[ini_size];
    array[0]=value;
    for(int i=1;i<(ini_size);i++)
    {
        array[i]=temp[i-1];
    }
    delete temp;
}

Last edited on
*ini_size++ will increment the pointer.

You can make it work the way you want by adding parentheses: (*ini_size)++
I believe the problem is this line :

*ini_size++;

It should be :

(*ini_size)++;

Since you need to first dereference the pointer and then increment the value it points to.
it worked. :) thanks.
Topic archived. No new replies allowed.