HUH...a[1] nto declared in main...

But what way to declare it? It is generated in po*?
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
  int* po(int b){
int d1;
d1=3*b;
int d2;
d2=d1+5;


int a[3];
a[0]=d1;
a[1]=d2;
a[2]=a[1]+3;


int * p;
p=new int[3];
cout<<"po: "<<p<<endl;
return p;
}


int main(){
int st=4;
cout<<" main: "<<po(st)<<endl<<endl<<endl;
cout<<a[1];

return 0;
}


1
2
3
In function 'int main()':
Line 24: error: 'a' was not declared in this scope
compilation terminated due to -Wfatal-errors.
In general, anything declared within the scope of a pair of opening and closing braces { } goes out of scope (is no longer accessible) after the closing brace. Example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
int main() 
{
    int x = 1;
    
    {
        int y = 2;
        cout << x << " " << y << endl; // ok, both x and y accessible
    } 

    cout << x << endl;  // ok, x is still available
    cout << y << endl;  // ERROR y not declared
    
    return 0;
}


In your example, the thing you try to use is inside another function, but the problem is the same.
Even with new???

So, what to do if I (still) want to save it somehow?
Last edited on
Several possibilities. You can return a value from a function using return or pass a parameter by reference using &.
Last edited on
Yes, of course, but I ment MORE then just one value.
I would need and array for that.
I can return one partucular value.

But can I return array anyhow?

many thanks!
Well, your function already does what you ask. The variable p is a pointer and the use of new means it points to an array of three integers.

The array a[3] is slightly different, in that it is a local variable declared within the function, so even if you had a pointer to a, it would no longer be valid when the function ends, as the memory used for a[3] is released and re-used for something else.
Chervil, THE PROBELM IS IT DOES NOT!!!
I get just pointer as pointer for the first element, and nth else!
So I tried with NEW to fix that array in memory in order to access its elements, but I did nto know how to declare them properly then...gr...look at this:


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
  int* po(int b){
int d1;
d1=3*b;
int d2;
d2=d1+5;


int a[3]={d1,d2,d2+3};
int * p=new int[3];

memcpy(p,a,3*sizeof(int));

cout<<"po: "<<p<<endl<<a[0]<<endl<<a[1]<<endl<<a[2]<<endl<<endl<<*p<<endl<<*(p+1)<<endl<<*(p+2)<<endl<<endl<<endl<<endl;;
return p;
}


int main(){
int st=5;
int z[3];

cout<<"main: "<<po(st)<<endl;
//cout<<z[1];


return 0;
}



I tried with ADDING : cout<<*p<<endl<<*(p+1)<<etc, BUT IT DID NOT WROK:BASH:BASH:BASH
Last edited on
I just wanted the function to RETURN MORE THEN ONE VALUE...can anyone give me at least a hint how to achieve this?


many thanks!
You havent properly read my answer in another Thread. It's difficult to help you this way.

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
 
int* po(int b){
int d1;
d1=3*b;
int d2;
d2=d1+5;
int d3;
d3 = d2+2;

// int a[3]={d1,d2,d2+3};  change this tooo see below
int* a = new int[3];  //This is allocated space that is not local and thus will keep existing 
// (even after function po has returned) as long as you don't release it 

//fill array here
a[0] = d1;
a[1] = d2;
a[2] = d3;

// don't know what you doing here but it's just unnecessary
// int * p=new int[3];   // remove this 
// this is not required either 
// memcpy(p,a,3*sizeof(int)); //remove this
/*
cout<<"po: "<<p<<endl<<a[0]<<endl<<a[1]<<endl<<a[2]<<endl<<endl<<*p<<endl<<*(p+1)<<endl<<*(p+2)<<endl<<endl<<endl<<endl;;
*/ //remove all this
// return p; change this to;
// you don't have to write an extra pointer "p", because "a" is already a pointer to your array
return a;
}


Okay I hope we set this right now.

ps: check class vector<> sometime (waaay easier with it)
Last edited on
Hello!
Thanks, Glandy!!!
We stil did not do clases, but will in a week or 2.

I did this, but why is z undeclared? What does it want now?

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

  int* po(int b){
int d1;
d1=3*b;
int d2;
d2=d1+5;
int d3;
d3=d2+3;
int* a=new int[3];


//int a[3]={d1,d2,d2+3};

a[0] = d1;
a[1] = d2;
a[2] = d3;


//cout<<"po: "<<p<<endl<<a[0]<<endl<<a[1]<<endl<<a[2]<<endl<<endl<<*p<<endl<<*(p+1)<<endl<<*(p+2)<<endl<<endl<<endl<<endl;;
return a;
}


int main(){
int st=5;
int z[3];

cout<<"main: "<<po(st)<<endl;
cout<<z[1];


return 0;
}
"a" is a pointer ,I still got in other (simioar) way, BUT what is the purpose of NEW if I cannot send the whole array from po to main?

In other words, I just want the functin to return MORE THEM ONE VALUE, and just did nto know how to do it without arrays.


po is returning a pointer now, NOT THE WHOLE ARRAY!!!

I will try to have a look to vectors now...
MANY THANKS!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int z[3]; 

cout<<"main: "<<po(st)<<endl; // this is just printing the address of your first element 
// inside your array
cout<<z[1]; // you've never initialized any fields of array z. 

//change stuff above to something like:

int* z;
z = po(st);

cout << "First: " << z[0] << endl;
cout << "Second: " << z[1] << endl;
cout << "Third: " << z[2] << endl;


Have fun and keep learning !
BINGO!!! THIS GLANDY JUST SOLVED THE PROBLEM!!!


First celebrating, then anything else:d!!!
The thing is, you have had all the necessary ingredients for success, but the way in which they were used wasn't giving a useful result.
Here's the code from the first post in this thread, re-arranged slightly:
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
#include <iostream>

using namespace std;

int* po(int b)
{
    int d1;
    d1 = 3*b;
    int d2;
    d2 = d1+5;
    
    int * a = new int[3];
    
    a[0] = d1;
    a[1] = d2;
    a[2] = a[1] + 3;
    
    return a;
}


int main()
{
    int st=4;
    int * a = po(st);
    cout << a[0] << endl;
    cout << a[1] << endl;
    cout << a[2] << endl;
    
    return 0;
}

Output:
12
17
20


There's nothing new here. But is a matter of understanding the effect of each line, not just what it is, but why. Putting things together in the right order is important.
Topic archived. No new replies allowed.