The top level value is passed into a function by value. That is the value is copied.
Consider this a crash course in basic pointers and pass by value.
You should really read something about it, although I couldn't recommend a good book as I just learnt things by playing around to see what happened.
Consider a more simple example:
char *szStr = "hello";
The variable szStr now contains a value that points to the memory where <code>"hello"</code> is stored.
For this example, we will say that this memory location is <code>0x1000</code>.
This means that at memory location <code>0x1000</code> - <code>0x1006</code> we have the characters 'h', 'e', 'l', 'l', 'o', '\0'
PrintStr(szStr);
void PrintStr(char *szPrint) {
++szPrint;
puts(szPrint);
szPrint[0] = 'E';
}
What is actually happening here is that we are copying the value of the pointer szStr (which is
0x1000
) onto the stack for the function PrintStr.
Because we have passed in a copy, any changes made to this copy of the pointer value wont be made to the value of szStr.
However, both
szStr
and the copy (
szPrint
parameter) contain 0x1000, and hence reference the same memory location.
This means that any changes made to the characters in
szPrint
will also be changed in
szStr
So, referring back to my first sentence, the top level here is the memory location that is stored in
szStr
and
szPrint
(0x1000).
No to your actual problem.
As has been previously suggested, you can make a tripple pointer and pass that in:
void main(){
double **data;
int rows=reading(2,&data);
}
int reading(int r,double ***data) {
if (r%2!=0) {
r++;
}
int rows=r;
delete[] *data;
*data=new double*[rows];
for(int k=0; k<rows; k++) {
(*data)[k]=new double[3];
}
for(int i=0; i<rows; i++) {
for(int j=0; j<3; j++) {
(*data)[i][j]=i+j;
}
}
return rows;
}
Or, since you indicated you are using C++, you can use pass by reference, which uses some clever tricks to not copy the value when passing it into the function, and also means that you dont need to worry about doing crazy things like
(*data)[i][j]=
This would look like:
void main() {
}
int reading(int r,double &**data) {
}