How to empty array in another function?

Hello!
I generated an array in pus function.
Then, I want to empty it in po fundtion.
Please, how to correct mistakes?
Many thanks!!!

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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
  #include<iostream>
using namespace std;


  int* pus(int b){
    int top;
    int max;
    max=5;

    int* a=new int[5];

    
      srand(time(0));
      int number;
      for(int i=0;i<top;i++){
      number=rand()%10;
      a[top]=number;
      }
return a;
}
      
int po(int* arraypo[5], int g){
  for(int i=5;i>=0;i++){
  arraypo[i]=0;
  }
return arraypo;
}     
      


int main(){
int st=5;
int * z;
z=pus(st);



cout<<"main: "<<pus(st)<<endl;
cout<<z[0]<<" "<<z[1]<<" "<<z[2]<<" "<<z[3]<<" "<<z[4]<<" "<<endl;
cout<<&z[0]<<" "<<&z[1]<<" "<<&z[2]<<endl;


z=pop(st);
cout<<z[0]<<" "<<z[1]<<" "<<z[2]<<" "<<z[3]<<" "<<z[4]<<" "<<endl;


delete z;
return 0;
}
 



Output:

	

In function 'int pop(int**, int)':
Line 29: error: invalid conversion from 'int**' to 'int'
compilation terminated due to -Wfatal-errors.

Last edited on
Use int* pop(int arraypo)
Hello!
I did, butnow it wants INITIALISER!
Still not that far...please, what to do?
Many thanks!

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
45
46
47
48
49
  #include<iostream>
using namespace std;


  int* pus(int b){
    int top;
    int max;
    max=5;

    int* a=new int[5];

    
      srand(time(0));
      int number;
      for(int i=0;i<top;i++){
      number=rand()%10;
      a[top]=number;
      }
return a;
}
      
int po*(int arraypo[5], int g){
  for(int i=5;i>=0;i++){
  arraypo[i]=0;
  }
return arraypo;
}     
      


int main(){
  int st=5;
  int * z;
  z=pus(st);



  cout<<"main: "<<pus(st)<<endl;
  cout<<z[0]<<" "<<z[1]<<" "<<z[2]<<" "<<z[3]<<" "<<z[4]<<" "<<endl;
  cout<<&z[0]<<" "<<&z[1]<<" "<<&z[2]<<endl;


  z=po(st);
  cout<<z[0]<<" "<<z[1]<<" "<<z[2]<<" "<<z[3]<<" "<<z[4]<<" "<<endl;


  delete z;
  return 0;
}
Do you see any difference between lines int* po( and int po*(?

Also your declaration is (int arraypo[5], int g) meaning that it takes an array and int, but in z=po(st); you are not providing any ints.
Hello!
Sorry, it was a typo, but good to leran sth new even on it.

When I corrected it I just got error :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
prog.cpp: In function ‘int* pus(int)’:
prog.cpp:7:9: warning: variable ‘max’ set but not used [-Wunused-but-set-variable]
     int max;
         ^
prog.cpp: In function ‘int main()’:
prog.cpp:43:16: error: expected primary-expression before ‘intint arraypo[5]=int z[5];
                ^
prog.cpp:44:8: error: invalid conversion from ‘int’ to ‘int*’ [-fpermissive]
 z=po(st);
        ^
prog.cpp:44:8: error: too few arguments to function ‘int* po(int*, int)’
prog.cpp:23:6: note: declared here
 int* po(int arraypo[5], int g){
z=po(st); too few arguments to function ‘int* po(int*, int)’
I believe it is well written and understandable.

prog.cpp:43:16: error: expected primary-expression before ‘int’
int arraypo[5]=int z[5];
Don't see that line in your code, but it is nonsensial.

Hello!
Please, first, why has first function pus() not been called TWICE now?
It should be called TWICE, shouldn+t it?

Please!!!
Please, show code and output where it is called only once. If you fix error with po() function, you should see that pus was called twice, unless you radically changed your code.
http://codepad.org/a5D2pgLN

P.s. hope I did not mistype now again!!!:bash

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
45
46
47
48
49
#include<iostream>
using namespace std;


  int* pus(int b){
    int top;
    int max;
    max=5;

    int* a=new int[5];

    
      srand(time(0));
      int number;
      for(top=0;top<max;top++){
      number=rand()%10;
      a[top]=number;
      }
return a;
}
      
int* po(int arraypo[5], int g){
  for(int i=5;i>=0;i++){
  arraypo[i]=0;
  }
return arraypo;
}     
      


int main(){
  int st=5;
  int * z;
  z=pus(st);



  cout<<"main: "<<pus(st)<<endl;
  cout<<z[0]<<" "<<z[1]<<" "<<z[2]<<" "<<z[3]<<" "<<z[4]<<" "<<endl;
  cout<<&z[0]<<" "<<&z[1]<<" "<<&z[2]<<endl;


  z=po(z, st);
  cout<<z[0]<<" "<<z[1]<<" "<<z[2]<<" "<<z[3]<<" "<<z[4]<<" "<<endl;


  delete z;
  return 0;
}
It does executes twice. But your po() function overwrites whole memory after arraypo resulting in crash. You probably meant:
1
2
3
4
5
6
7
//I still don't know why do you need g variable
int* po(int arraypo[5], int g){
    for(int i = 0;i < 5; ++i) {
        arraypo[i]=0;
    }
    return arraypo;
} 
Hello:)!!!
THAT would be what I acutally wanted to achieve.
But:

What is the error et the end of compliation, the hell???
(at least the program WAS executed!!!)

http://ideone.com/GAysOP

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
45
46
47
48
49
50
51
52
53
54
55
#include<iostream>
using namespace std;


int* pus(int b){
  int top;
  int max;
  max=5;

  int* a=new int[5];

    
  srand(time(0));
  int number;
  for(top=0;top<max;top++){
  number=rand()%10;
  a[top]=number;
      }
return a;
}
      
int* po(int arraypo[], int g){
  for(int i=g;i>=0;i--){
  arraypo[i]=0;
  }
return arraypo;
}     
      


int main(){
  int st=5;
  int * z;
  z=pus(st);



  cout<<"main: "<<pus(st)<<endl;
  cout<<z[0]<<" "<<z[1]<<" "<<z[2]<<" "<<z[3]<<" "<<z[4]<<endl;
  cout<<&z[0]<<" "<<&z[1]<<" "<<&z[2]<<" "<<&z[3]<<" "<<z[4]<<endl;


  z=po(z, st);
  cout<<z[0]<<" "<<z[1]<<" "<<z[2]<<" "<<z[3]<<" "<<z[4]<<" "<<endl;
  cout<<&z[0]<<" "<<&z[1]<<" "<<&z[2]<<" "<<&z[3]<<" "<<&z[4]<<endl;



  delete z;
  return 0;
} 



Last edited on
1) for(int i=g [b]- 1[/b];i>=0;i--){ You do not have arraypo[5] element actually
2) delete[] z; z is pointing to an array, so you have to use array delete.
Now it WORKS!!! THNANKS!!!
Topic archived. No new replies allowed.