Long story short i'm trying to write a program that solves matrix multiplication recursively. As part of this I want a function that spawns off mini arrays.
So below is a function that returns a pointer to a multi-dimensional array. It returns a pointer to my 1 by 1 array...and all is dandy.
However, I set zipbop[0][0] = 60' in the function.
1) Why does *test not equal 60 in main? (i get *test == 32 when I run this code in cygwin)
2) Why can I not get the value of test[0][0] in main?
#include <iostream>
usingnamespace std;
int * createarray(int n) {
int zipbop[n][n];
zipbop[0][0] = 60;
cout << "this is the address,1st way "<< *zipbop << endl;
cout << "this is the address, 2nd way "<< zipbop << endl;
cout << "this is the address, 3rd way "<< &zipbop << endl;
cout << "this is the value, 1st way "<< zipbop[0][0] << endl;
cout << "this is the value, 2nd way "<< **zipbop << endl;
cout << endl;
int *help;
help = *zipbop;
return help;
}
int main () {
int *test;
test = createarray(1);
cout << "this is the address " << test << endl;
cout << "this is the value " << *test << endl;
return 0;
}
Anyway, what if i change that to int zipbop[1][1];
i am still unable to return '60' in main, when i deference the pointer. Here is my output:
1 2 3 4 5 6 7 8
this is the address,1st way 0x5fcaa0
this is the address, 2nd way 0x5fcaa0
this is the address, 3rd way 0x5fcaa0
this is the value, 1st way 60
this is the value, 2nd way 60
this is the address 0x5fcaa0
this is the value -2144153192
if your compiler is giving yo ua random number, this means there was a data loss and the compiler didnt receive the correct value, so it will output just garbage
If you really want to use 2D arrays of variable size, this will have to be done dynamically.
And in your function, you declare zipbop as a 2D array, yet you return a int* pointer, which is equivalent to returning a 1D array instead. You won't get expected output.
This does not help. You, or your friends, or whoever, did this all last night and caused confusion with those who needed help. We have to work to undo whatever confusion you've caused.
@niko79542 You just have to be careful with allocation and deletion. But I hope that helps you with your recursive matrix stuff. Feel free to ask more questions if you have any.