Returning a Pointer to an array

Hi,

I'm trying to write a function which generates and returns an array. I know I can't actually return the array and instead need to return a pointer, but I'm still confused about how pointers work. What I've done is the following:

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
#include<cstdlib>
#include <time.h>
#include<math.h>
#include<iomanip>
#include<ctime>
using namespace std;


int* gen_graph(int n, float p)
    {
    srand (time (NULL));
    int adj[n][n];
    int i = 0;
    while (i < n)
        {
        adj[i][i] = 0;
        int j = i + 1;
        while (j < n)
            {
            int A = rand();
            float B = A/RAND_MAX;
            if (B < p)
                {
                adj[i][j] = 1;
                }
            else{
                adj[i][j] = 0;
                }
            adj[j][i] = adj[i][j];
            j++;
            }
        i++;
        }
    int *a;
    a = adj;
    return a;
    }
            


I'm getting the following error message:

1
2
3
4
5
6
user@dell:~$ g++ ~/graphs.cpp
/home/user/graphs.cpp: In function ‘int* gen_graph(int, float)’:
/home/user/graphs.cpp:44: error: cannot convert ‘int 
[(((unsigned int)(((int)n) + -0x00000000000000001)) + 1)]
[(((unsigned int)(((int)n) + -0x00000000000000001)) + 1)]’ 
to ‘int*’ in assignment


Could someone explain where I'm going wrong?

Thanks!
adj is a 2D array, so it's an int**.
But it will go out of scope at the end of the function so you won't be able to return it unless you allocate it dynamically ( which you will need to do anyway since it size depends on an argument )
Try;
a = reinterpret_cast<int *>(adj);
This will only help you to prevent this compiler error , but as Bazzy said, you shall allocate memory dynamically to make your function work.
Last edited on
Thanks for the quick replies! I've made it an int**, and tried modifying my code to use dynamic memory -- but I'm getting the error message

/home/user/graphs.cpp:15: error: ‘n’ cannot appear in a constant-expression.

I've never used dynamic memory before -- can someone explain a little how it works?

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
int** gen_graph(int n, float p)
    {
    srand (time (NULL));
    int ** adj;
    adj = new int[n][n];
    int i = 0;
    while (i < n)
        {
        adj[i][i] = 0;
        int j = i + 1;
        while (j < n)
            {
            int A = rand();
            float B = A/RAND_MAX;
            if (B < p)
                {
                adj[i][j] = 1;
                }
            else{
                adj[i][j] = 0;
                }
            adj[j][i] = adj[i][j];
            j++;
            }
        i++;
        }
    int **a;
    a = adj;
    return a;
    }


Thanks!
Thanks for those links. I'm afraid I'm still confused -- let me see if I can articulate why:

In the example given towards the bottom of http://www.cplusplus.com/doc/tutorial/dynamic/ , the value in the new statement is a variable value entered by the user (i), not a constant value. However, in my program, even when I modify it based on this example to prompt the user to input n instead of declaring n as an argument, and then say adj = new int[n][n]; , I get the error "n cannot appear in a constant-expression".

What's the difference between these situations?

You need operator new two separate times, once for each array. Here are a couple of more articles to read. Although I do not agree that MD arrays are evil, the second article still introduces some interesting ideas.
http://cplusplus.com/forum/articles/20881/
http://cplusplus.com/forum/articles/17108/

I'm not convinced that the function in question needs to actually construct the array. I would encourage you to do a couple of things. b will alleviate some of your concerns with regards to how to return the array while a allows you to make the array without worrying about operator new or delete.
a) use sequence containers such as std::vector.
b) create the object within the caller and simply pass a reference to the gen_graph function.


Topic archived. No new replies allowed.