Dynamic Arrays

I'm writing a function to set up a dynamic array and a separate function to to print out this dynamic array. my problem is the print function wont recognize my array, do i have to pass the array back when i initialize the array or should a dynamic array not have to be passed because its a reference variable?
your advice would be much appreciated,
thanks
If you function dynamically creates the array, then it needs to return to the caller two things: a pointer to the array and the number of elements in the array.
thanks still couldnt get it to work, This is my code and these are the errors that are being produced.
I've been working on it for awhile and I can for the life of me figure out how to get it working
what might be causing these errors?

inline int*& Module::modulearray(void)
{

struct Modules //struct declaration
{
int modulenum;
char *startdate;
char *enddate;
};

//dynamic array declaration
Modules *mods = new Modules[9];
Modules *ModulesPointer = &mods[0]; //pointer to a Modules data object

//structure initialization one by one
(*ModulesPointer).modulenum = 1;
ModulesPointer->startdate = "22/09/09";
ModulesPointer->enddate = "15/12/09";

ModulesPointer = &mods[1];
(*ModulesPointer).modulenum = 2;
ModulesPointer->startdate = "22/09/09";
ModulesPointer->enddate = "15/12/09";

ModulesPointer = &mods[2];
(*ModulesPointer).modulenum = 3;
ModulesPointer->startdate = "22/09/09";
ModulesPointer->enddate = "15/12/09";

ModulesPointer = &mods[3];
(*ModulesPointer).modulenum = 4;
ModulesPointer->startdate = "22/09/09";
ModulesPointer->enddate = "15/12/09";

ModulesPointer = &mods[4];
(*ModulesPointer).modulenum = 5;
ModulesPointer->startdate = "22/09/09";
ModulesPointer->enddate = "15/12/09";

ModulesPointer = &mods[5];
(*ModulesPointer).modulenum = 6;
ModulesPointer->startdate = "22/09/09";
ModulesPointer->enddate = "15/12/09";

ModulesPointer = &mods[6];
(*ModulesPointer).modulenum = 7;
ModulesPointer->startdate = "22/09/09";
ModulesPointer->enddate = "15/12/09";

ModulesPointer = &mods[7];
(*ModulesPointer).modulenum = 8;
ModulesPointer->startdate = "22/09/09";
ModulesPointer->enddate = "15/12/09";

ModulesPointer = &mods[8];
(*ModulesPointer).modulenum = 9;
ModulesPointer->startdate = "22/09/09";
ModulesPointer->enddate = "15/12/09";

ModulesPointer = &mods[9];
(*ModulesPointer).modulenum = 10;
ModulesPointer->startdate = "22/09/09";
ModulesPointer->enddate = "15/12/09";

return ModulePointer[9];

}


Line Location Module.h:163: error: 'ModulePointer' was not declared in this scope
Line Location Module.h:231: error: 'mods' was not declared in this scope
Line Location Module.h:231: error: 'ModulesPointer' was not declared in this scope

Last edited on
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
68
69
70
71
inline int*& Module::modulearray(void)
{

struct Modules //struct declaration
{
int modulenum;
char *startdate;
char *enddate;
};

//dynamic array declaration
Modules *mods = new Modules[9];
Modules *ModulesPointer = &mods[0]; //pointer to a Modules data object

//structure initialization one by one
(*ModulesPointer).modulenum = 1;
ModulesPointer->startdate = "22/09/09";
ModulesPointer->enddate = "15/12/09";

ModulesPointer = &mods[1];
(*ModulesPointer).modulenum = 2;
ModulesPointer->startdate = "22/09/09";
ModulesPointer->enddate = "15/12/09";

ModulesPointer = &mods[2];
(*ModulesPointer).modulenum = 3;
ModulesPointer->startdate = "22/09/09";
ModulesPointer->enddate = "15/12/09";

ModulesPointer = &mods[3];
(*ModulesPointer).modulenum = 4;
ModulesPointer->startdate = "22/09/09";
ModulesPointer->enddate = "15/12/09";

ModulesPointer = &mods[4];
(*ModulesPointer).modulenum = 5;
ModulesPointer->startdate = "22/09/09";
ModulesPointer->enddate = "15/12/09";

ModulesPointer = &mods[5];
(*ModulesPointer).modulenum = 6;
ModulesPointer->startdate = "22/09/09";
ModulesPointer->enddate = "15/12/09";

ModulesPointer = &mods[6];
(*ModulesPointer).modulenum = 7;
ModulesPointer->startdate = "22/09/09";
ModulesPointer->enddate = "15/12/09";

ModulesPointer = &mods[7];
(*ModulesPointer).modulenum = 8;
ModulesPointer->startdate = "22/09/09";
ModulesPointer->enddate = "15/12/09";

ModulesPointer = &mods[8];
(*ModulesPointer).modulenum = 9;
ModulesPointer->startdate = "22/09/09";
ModulesPointer->enddate = "15/12/09";

ModulesPointer = &mods[9];
(*ModulesPointer).modulenum = 10;
ModulesPointer->startdate = "22/09/09";
ModulesPointer->enddate = "15/12/09";

// There is no ModulePointer, only a ModulesPointer
// This is wrong anyhow since the function is supposed to return a reference
// to an integer pointer.  It is also wrong because according to your requirement 
// you want to return the entire array.  
return ModulePointer[9];

}


You can do one of two things.
a) Change the function declaration to return void. Have the caller pass in two reference parameters, the first being a reference to a Modules*, and the second an int that will hold the size of the array.

b) Create a std::vector of Modules or Modules*. (better)

Also, the struct type name shouldn't be plural. The array name is plural. However each instance of the struct is one single module.
Last edited on
Topic archived. No new replies allowed.