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.
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:
int* po(int b){
int d1;
d1=3*b;
int d2;
d2=d1+5;
int a[3]={d1,d2,d2+3};
int * p=newint[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
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 = newint[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)
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;
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:
#include <iostream>
usingnamespace std;
int* po(int b)
{
int d1;
d1 = 3*b;
int d2;
d2 = d1+5;
int * a = newint[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.