Passing a pointer to a function

Pages: 12
I've made a function to set up a array of modules and then a separate function to print out these modules how would i go about passing the pointer to the array to the print function?

These are the errors i get with the code when i compile it:
Line Location Module.h:165: error: 'ModulesPointer' was not declared in this scope
Line Location Module.h:165: error: 'mods' was not declared in this scope

These are the functions:
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
 
inline void Module::modulearray(void)
{
	struct module	//struct declaration
	{
	int modulenum;
	char* startdate;
	char* enddate;
	};
 
	//dynamic array declaration
	module *mods = new module[MAXMOD];
	module *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";
	
}


inline void Module::printarray(void)
{
	
 //data output to the screen
 
 //first element of the array
 ModulesPointer = &mods[0];
 cout << "Module: " << (*ModulesPointer).modulenum << endl;
 cout << "Start Date: " << ModulesPointer->startdate << endl;
 cout << "End Date: " << ModulesPointer->enddate << "\n\n"; 
 
 //second element of the array
 ModulesPointer = &mods[1];
 cout << "Module: " << (*ModulesPointer).modulenum << endl;
 cout << "Start Date: " << ModulesPointer->startdate << endl;
 cout << "End Date: " << ModulesPointer->enddate << "\n\n";
 
 //third element of the array
 ModulesPointer = &mods[2];
 cout << "Module: " << (*ModulesPointer).modulenum << endl;
 cout << "Start Date: " << ModulesPointer->startdate << endl;
 cout << "End Date: " << ModulesPointer->enddate << "\n\n";
 
 ModulesPointer = &mods[3];
 cout << "Module: " << (*ModulesPointer).modulenum << endl;
 cout << "Start Date: " << ModulesPointer->startdate << endl;
 cout << "End Date: " << ModulesPointer->enddate << "\n\n"; 
 
 //second element of the array
 ModulesPointer = &mods[4];
 cout << "Module: " << (*ModulesPointer).modulenum << endl;
 cout << "Start Date: " << ModulesPointer->startdate << endl;
 cout << "End Date: " << ModulesPointer->enddate << "\n\n";
 
 //third element of the array
 ModulesPointer = &mods[5];
 cout << "Module: " << (*ModulesPointer).modulenum << endl;
 cout << "Start Date: " << ModulesPointer->startdate << endl;
 cout << "End Date: " << ModulesPointer->enddate << "\n\n";
 
 ModulesPointer = &mods[6];
 cout << "Module: " << (*ModulesPointer).modulenum << endl;
 cout << "Start Date: " << ModulesPointer->startdate << endl;
 cout << "End Date: " << ModulesPointer->enddate << "\n\n";
 
 ModulesPointer = &mods[7];
 cout << "Module: " << (*ModulesPointer).modulenum << endl;
 cout << "Start Date: " << ModulesPointer->startdate << endl;
 cout << "End Date: " << ModulesPointer->enddate << "\n\n"; 
 
 //second element of the array
 ModulesPointer = &mods[8];
 cout << "Module: " << (*ModulesPointer).modulenum << endl;
 cout << "Start Date: " << ModulesPointer->startdate << endl;
 cout << "End Date: " << ModulesPointer->enddate << "\n\n";
 
 //third element of the array
 ModulesPointer = &mods[9];
 cout << "Module: " << (*ModulesPointer).modulenum << endl;
 cout << "Start Date: " << ModulesPointer->startdate << endl;
 cout << "End Date: " << ModulesPointer->enddate << "\n\n";
 }

Sorry I'm new didn't realize how to do that
Last edited on
Its Ok !
Last edited on
I'm pretty sure you can't declare named structures inside a function.
This is legal: struct {int a;} a;
But this isn't:
1
2
3
4
struct a{
    int a;
};
a b;


By the way, &pointer[0] is the same as pointer. Think about it.
the structure works fine, its perfectly legal.
i'm not sure what you mean when you say that, is it that returning mods is that same as &mods[MAXARRAY]?
the structure works fine, its perfectly legal.
Well, the compiler doesn't seem to think so.

Never mind. I found the problem. On line 74 of the code you posted, neither of the two identifiers were declared. The ones declared in modulearray() are local to that function. You can't use those in main().
This would have been easier if you had posted the exact code you gave the compiler.

I mean that
1
2
T a[40]={0};
T *b=&a[0];
is the same as
1
2
T a[40]={0};
T *b=a;
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
int main ()
{
	
	Module module;
	
	module.modulearray();
	
	module.printarray();

	return 0;

}


This the code that wass in the cpp file when i compi;ed and got the errors
Sorry, I read the code wrong. I meant to say Module::printarray(), not main().
module *mods = new module[MAXMOD];

mods is local to the function that builds the array. In the main you are calling moduleArray() which creates and leaks memory. Then you call printArray from the main. The memory is leaked so there is no way to pass it to printarray unless you call printarray from module array. Since they are both members, you could make the array a class attribute. Since it is dynamic, use a std::vector if you are allowed to. If the array is part of the class instance then you don't have to pass it through the function. printArray can simply print the classes current array by accessing the member.

I cannot emphasize enough how important it is to copy and paste the example into this site so that we can see exactly what is being fed to the compiler.
Returning mods seems like the simplest way, to me, although I don't really know what you have in mind for that array. Perhaps it would be better to make it a member of the class, but I can't know that.

Also, I just noticed two other things:
1. You seem to inline all functions/methods.
2. You don't seem to know about for.

EDIT: Huh? Where did it go?
Last edited on
the program goes like this.
There area 15 students, 10 modules and 5 lecturers.
The program read in the 15 students names and the user assigns two modules to each student then the program prints out the students name and the two modules and the lecturers (each lecturer has two modules) and start times associated with them.
Its to do with inheritance and i'm finding it awfully confusing.

I don't really know what you have in mind for that array.

I'm trying to sort of hard code in the modules

1. You seem to inline all functions/methods.
2. You don't seem to know about for.

I wasnt aware of another way this is how we were told in college
I dont get what you mean by the other point?
Last edited on
2. You don't seem to know about for.
1
2
3
4
5
6
for(int i = 0; i < 10; ++i)
{
    cout << "Module: " << mods[i].modulenum << endl;
    cout << "Start Date: " << mods[i].startdate << endl;
    cout << "End Date: " << mods[i].enddate << "\n\n";
}


That's the concept.
no i'm aware of it i just didnt use it because it would just assign a number for date not the date which is what i wanted.
Last edited on
Um... What?
1
2
3
4
5
for (int i=0;i<MAXMOD;i++){
    mods[i].modulenum = i;
    mods[i].startdate = "22/09/09";
    mods[i].enddate = "15/12/09";
}
Last edited on
yea i could do that but i wanted different dates so i did it the way i did.
Does anyone know how i would go about passing the array to the print function?
any suggestions would be much appreciated thanks!
i included std::vector in the .cpp but now it doesnt like my constructors and destructors and when i put it in the .h file it throws up a whole lot of errors
1
2
3
4
void Module::printarray(module* theModules)

// in the other function
printArray(mods);


By the way, reassigning mods to another temporary pointer variable isn't helping. Just use mods directly. Good luck.
If you want to pass the array that you create in modulearray() to printarray(), you'll have to modify the signature of your funtion printarray() like this:

inline void Module::printarray(module * PointerToArray);
Now the function can accept a pointer to a module object as argument.

You'll need to pass the array as this argument that we are talking about.

You really do not need to do this on line 13:
 
module *ModulesPointer = &mods[0];


because when you do this on line 12:
module *mods = new module[MAXMOD];
You already create a pointer to the start of your array; that pointer is mods.

So you want to pass mods to printarray(). Here's how you do it:
 
printarray(mods);


Pretty simple? I think it will be for you if you try to understand the relationship between arrays and pointers well.

Hope this helps and good luck ;)
But passing the array back from modulearray what would the function prototype look like

? modulearray(void);

i think if i manage to get this one piece of code work i can easily get the whoike program to work... i hope
Last edited on
How do i pass mods back i cant get it working heres what i'm give the compiler i'm just using th function to intialize the array and using one of the elements and then trying to print it in main.
The problem is it doesn't recognise mods in main how do i return mods (pointer) from my function? any advice is very welcome, thanks

error: 'mods' was not declared in this scope Line Location DdateV001.cpp:57:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
 
int main ()
{
	
	Module modules;
	
	modules.modulearray();
	
	cout << "Module: " << mods[0].modulenum << endl;
	cout << "Start Date: " << mods[0].startdate << endl;
	cout << "End Date: " << mods[0].enddate << "\n\n"; 
	
	
	//modules.printarray(mods);
	
	
	return 0;

}


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
class Module
{

public:
	
	Module (void);
	~Module(void);
	Module(int num, char* start, char* end);
	void modulearray(void);
	void printarray(char * mods);
	
private:
	
	typedef struct 	//struct declaration
		{
			int modulenum;
			char* startdate;
			char* enddate;
		}list;

	int modulenum;
	char startdate[MAXNAME];
	char enddate[MAXNAME];
	
};


inline void Module::modulearray(void)
{
	
	//dynamic array declaration
	list *mods = new list[MAXMOD];
	
	//structure initialization one by one
	mods[0].modulenum = 1;
	mods[0].startdate = "22/09/09";
	mods[0].enddate = "15/12/09";
	
	
	
}
Last edited on
Pages: 12