Function to return pointer to multi-dimensional array

Dec 10, 2015 at 8:17pm
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?

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 * 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;
}
Dec 10, 2015 at 8:23pm
int zipbop[n][n];

How is that compiling? This is not allowed. The size of a static array must be known at compile time.

http://www.cplusplus.com/doc/tutorial/arrays/
Dec 10, 2015 at 8:33pm
wierd, it compiled for me.

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
Dec 10, 2015 at 8:47pm
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

have you tried just

help = zipbop?

why don't you just return zipbop?
Dec 10, 2015 at 8:56pm
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.
Dec 10, 2015 at 8:59pm
Here, this an example of how to create a 2D array, and return it to a 2D pointer in main

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
58
59
60
61
62
63
64
65
66
67
#include <iostream>
#include <iomanip>
using namespace std;

int** createarray(int n) {

	int **arr = new int*[5];

	for (auto i = 0; i < 5; i++)
	{
		arr[i] = new int[5];
	}

	//Assign Values to matrix
	for (auto i = 0; i < 5; i++)
	{
		for (auto j = 0; j < 5; j++)
		{
			arr[i][j] = i * j;
		}
	}

	//Print matrix
	for (auto i = 0; i < 5; i++)
	{
		//New row
		std::cout << "|";
		for (auto j = 0; j < 5; j++)
		{
			std::cout << std::setw(4) << arr[i][j] << "|";
		}
		std::cout << std::endl;
	}

	return arr;

}

int main() {
	int ** test;
	test = createarray(1);

	cout << "this is the address " << test << endl;
	
	for (auto i = 0; i < 5; i++)
	{
		std::cout << "Row " << i << std::endl;
		std::cout << "Starting address: " << test[i] << std::endl;
		std::cout << "| ";
		for (auto j = 0; j < 5; j++)
		{
			std::cout << "&arr[" << i << "][" << j << "]: " << &test[i][j] << " | ";
		}
		std::cout << std::endl;
	}

	//You need to delete the array

	for (auto i = 0; i < 5; i++)
	{
		delete[] test[i];
	}

	delete[] test;

	return 0;
}


It produces this output

|   0|   0|   0|   0|   0|
|   0|   1|   2|   3|   4|
|   0|   2|   4|   6|   8|
|   0|   3|   6|   9|  12|
|   0|   4|   8|  12|  16|
this is the address 007B8550
Row 0
Starting address: 007B9C68
| &arr[0][0]: 007B9C68 | &arr[0][1]: 007B9C6C | &arr[0][2]: 007B9C70 | &arr[0][3]: 007B9C74 | &arr[0][4]: 007B9C78 |
Row 1
Starting address: 007B9440
| &arr[1][0]: 007B9440 | &arr[1][1]: 007B9444 | &arr[1][2]: 007B9448 | &arr[1][3]: 007B944C | &arr[1][4]: 007B9450 |
Row 2
Starting address: 007BA2C8
| &arr[2][0]: 007BA2C8 | &arr[2][1]: 007BA2CC | &arr[2][2]: 007BA2D0 | &arr[2][3]: 007BA2D4 | &arr[2][4]: 007BA2D8 |
Row 3
Starting address: 007BA308
| &arr[3][0]: 007BA308 | &arr[3][1]: 007BA30C | &arr[3][2]: 007BA310 | &arr[3][3]: 007BA314 | &arr[3][4]: 007BA318 |
Row 4
Starting address: 007C0F80
| &arr[4][0]: 007C0F80 | &arr[4][1]: 007C0F84 | &arr[4][2]: 007C0F88 | &arr[4][3]: 007C0F8C | &arr[4][4]: 007C0F90 |
Press any key to continue . . .
Dec 10, 2015 at 9:03pm
Trolls are back.
Disregard comments from asl1
Last edited on Dec 10, 2015 at 9:03pm
Dec 10, 2015 at 9:07pm
Thanks JZ!
Dec 10, 2015 at 9:08pm
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.
Dec 10, 2015 at 9:09pm
@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.
Topic archived. No new replies allowed.