invalid conversion int to int*, 53 line

Hello!
Please, does someone see what is wrong here?

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
  #include<iostream>
using namespace std;
 
int*  f2(int z){
int* str=new int[40];
int s=-1;
 srand(time(0));
 
int zn=0;
 
int random_integer; 
    for(int index=0; index<10; index++){ 
        random_integer = (rand()%(z));   
        str[index]= random_integer;
 		cout<<"str["<<index<<"]="<<random_integer<<endl;
 		s=s+1;
 zn=zn+random_integer;
if (zn>3*z){cout<<"Last value is: "<<random_integer<<endl<<"Whole sum is: "<<zn<<endl<<"Index number: "<<s;break;}
 
 } 
return str; 
delete[]str;
}
 
 
void sort(int po[40], int do2) {           
   for (int p=1; p < do2; p++) {
       for (int i=0; i < do2-p; i++) {
           if (po[i] < po[i+1]) {
               int temp = po[i]; 
               po[i] = po[i+1]; 
               po[i+1] = temp;
           }
       }
   }
   for(int t=0;t<do2;t++){cout<<po[t]<<endl;}
}
 
 
 
int main(){
int doh=30;
int tre=doh/3;
int vp=40;
 
int* z;
z=f2(tre);
 
 cout<<endl<<endl<<endl;
 
 
// for(int h=0;h<40; h++) {cout<<z[h]<<endl;}
 sort(z[40], vp);
 
 delete[]z;
return 0;
}
prog.cpp:53:15: error: invalid conversion from ‘int’ to ‘int*’ [-fpermissive]

prog.cpp:26:6: error: initializing argument 1 of ‘void sortiraj(int*, int)’ [-fpermissive]
The problem is that you're trying to pass an int into a function, but you've defined the function to take an array of ints.
Plesase, what shoudl I do? HOw to correct it?
I am trying to pass to the function one array AND one int!!!
Then pass the array and not an element from the array. In other words, drop the element access operator '[]' when you're passing the argument.
Hey, THANKS!!!:TUP!!!
Then pass the array and not an element from the array. In other words, drop the element access operator '[]' when you're passing the argument.

Exactly.

In fact, z[40] isn't even an element of the array. It would be the 41st element of the array, but the array is only defined to have 40 elements:

 
int* str=new int[40];


... so it's beyond the end of the array.

Also, enemy, what's the point of:

1
2
3
return str; 
delete[]str;
}


? When the return statement executes, that function ends - the delete statement will never be executed.

And lucky for you that it isn't executed, because if it was, it would delete the memory allocated to the array, so the pointer becomes invalid.

Last edited on
Hello!
U just opened another question I wanted to ask!!!

This is the nice example for q, we SHOULD delete the stuff, but we do not want.

Please, how to solve this problem???

(ps.. I knew what I wanted to achieve when I wrote it...but this sounds very tricky for me...)


Many thanks!!!
Topic archived. No new replies allowed.