Having trouble assigning value to an array

Pages: 123
Why am I passing pointers?
I couldn't figure out another way to get the pointers into the function to add the two arrays together. I'm alittle ignorant about any other way to get the arrays into the function to get it to work.

So what you're saying is the fact that I have the functions below main and am calling them back to main is the reason I'm having trouble getting it to execute?

I'll give it a shot.
Alright the problem I'm having isn't what you're examples laid out. Or I wasn't able to get my answer out of them. I got how what you said works in the example because I'm doing that. The problem is when the functions have arguments (hope I'm using the right term) I can't get it to execute in main without an error. Maybe it's a problem with using pointers as the argument?

void f(int *pointer, int* pointer2);

int main(){
f(*pointer, *pointer2);
}

void f(int *pointer, int *pointer2){
something;
}

keeps telling me there's a problem with the function under main.

error C2144: syntax error : 'int' should be preceded by ')'
error C2660: 'function does not take 0 arguments
error C2059: syntax error : ')'
You do realize that an array is a pointer to some data, right?

I.e.:
1
2
char* x; //might as well be
char x[];


In fact, you can you the [] to access pointers, just access element 0, although it is obviously a stupid thing to do.

The reason why it doesn't work is because the compiler reads "down" the page basically.

Here is some examples (basically like helios's :P):

1
2
3
4
5
6
7
8
void f() { //compiler gets function f()
   return 0;
}

int main() {
   f(); //compiler sees this being called, and knows what it is
   return 0;
}


1
2
3
4
5
6
7
8
int main() {
   f(); //compiler sees this being called, and doesn't know that it exists yet, so it throws an error
   return 0;
}

void f() { //compiler doesn't read here for the above
   return 0;
}


However, there is a way around this (like helios showed) called prototyping:

1
2
3
4
5
6
7
8
9
10
void f(); //prototype, tells the compiler that the function is defined, just not here

int main() {
   f(); //compiler sees this being called, and knows it exists, but it hasn't found it yet
   return 0;
}

void f() { //compiler sees this and goes "oh, that is the function I saw before"
   return 0;
}
Yeah, I get the idea of prototypes. I'm using a few. The problem I'm having is getting a prototype to work when it has the pointers in as an argument.

using the example:
1
2
3
4
5
6
7
8
9
void f(int *pointer, int* pointer2);

int main(){
f(*pointer, *pointer2);
}

void f(int *pointer, int *pointer2){
something;
}


it hits me with a bunch of errors:
error C2144: syntax error : 'int' should be preceded by ')'
error C2660: 'function does not take 0 arguments
error C2059: syntax error : ')'

all for the line in main. If I take the line out of main, it works fine but won't execute ya know?


Last edited on
f(*pointer, *pointer2);

Well, it seems you don't really know how pointers work...read up on this:
http://www.cplusplus.com/doc/tutorial/pointers.html
Well if I don't understand something I would think it's really this particular problem because I've read that tutorial already. I'll re-read it, see if I can figure out the problem but I don't know. I'm obviously missing something.

Thanks for the help thus far.
What's up with those errors? "int should be preceded by ')'"? "Function does not take 0 arguments"? Did my internal compiler broke or are those just pure nonsense?

Also, to access the first element of an array, you can use *:
1
2
int a[10];
*a=0;
I always do that because each keystroke sends a wave of pain down my spine.

EDIT: Damn. So slow.
Last edited on
1
2
3
4
5
6
7
8
9
void f(int x, int y);

int main(){
f(x, y);
}

void f(int *pointer, int *pointer2){
something;
}


Something like that should work, because I can't get that to work either :/
Last edited on
Here's my actual code, I've rewritten the whole thing about 10 times lol
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
#include <iostream>
#include <math.h>
#include <iomanip> 

using namespace std;
int const MAXSTUDENTS = 100;
int getStudentCount();
int noofstu;
int getExamScores();
int num;
int getLabScores();
calculatePointGrades(*Examscores[i], *Labscores[i]);//No Problems recorded here but I think there's something wrong.
int i;




int main ()
{ //Open main
	

	getStudentCount();
	getExamScores();
	getLabScores();
	calculatePointGrades( *Examscores[i], *Labscores[i]);// Problems here
	

	cin.ignore(2);
return 0;
} // Close main

int getStudentCount()
{
	cout << "Calculating Students Grades" << endl;
	cout << "\nHow many students grades would you like to enter today? (Up to 100)\n" << endl;
		cin >> noofstu;
		return 0;
}

int getExamScores()
{
	
	int * Examscores;
	Examscores = new int [noofstu];
	
	if (noofstu == 0)
	{
		cout << "You have entered no students, try again" << endl;
	}
	else 
	{ 
		for (num=0; num < noofstu; num++)
		{
			int n = num + 1;
	cout << "\nEnter Exam Score(s) for student "<< n++ << endl;
			cin >> Examscores[num];
		}
		cout << "You've entered: ";
		for (num=0; num < noofstu; num++)
			cout << Examscores[num]<< ", ";
		cout << "\n" << endl;
	}
	return 0;
}

int getLabScores()
{

int * Labscores;
Labscores = new int [noofstu];

for (num =0; num < noofstu; num++)
{
	int n = num + 1;
	cout << "\nEnter Lab Score(s) for student "<< n++ << endl;
	cin >> Labscores[num];
}
cout<< "You Entered:";
for (num=0; num < noofstu; num++)
cout << Labscores[num]<< ", ";


return 0;
}


int calculatePointGrades(int * Examscores[], int * Labscores[])
{

	int * Pointgrades;
	Pointgrades = new int [noofstu];
	int sum = 0;

	for (int i=0; i<noofstu; i++)
	{
	 Pointgrades[i] = * Examscores[i]+ * Labscores[i]; 
	}
	return 0;
}


Some things declared at the top are left over from other attempts.
And I know why firedraco is saying that I don't understand, I think. I'm using pointers to declare something, but I've tried aobut everything else I could think of.
Last edited on
I found this website helpful with pointers and a few other things. http://www.cprogramming.com/tutorial.html It actually covers a lot of things and explains them pretty well.
Line 12: Almost everything in this line is wrong. No return type, parameters are pointers to something with no type, there's a variable between the brackets. I rewrote it like this. I don't know if this is what you meant.
int calculatePointGrades(int *Examscores[],int *Labscores[]);
Line 25: Neither Examscores nor Labscores are declared. Also, you don't seem to understand how to pass pointers. Once those variables are declared somewhere, this line should be rewritten as
calculatePointGrades(Examscores, Labscores);

getExamScores() should return a pointer to an int array, not an int. Since you're not returning anything meaningful, you're not modifying parameters, and you're not changing global states, the entire function does nothing.
The same goes for getLabScores() and calculatePointGrades()

I recommend you read this: http://newdata.box.sk/bx/c/htm/ch08.htm
Do not attempt to do anything else until you've finished reading it and understand the concept. You'd just be wasting time.
Last edited on
Yeah I'm missing something, so once again thanks for your help thus far.
I'll read up and see what I can get.
I'll post later, after I've read more
Last edited on
Alright I read it and it did help alot. I understand the way it works far more now then I did before. I'm attempting to fix the code. Still running into a few problems though.

Haven't really done to much to the code yet but I'm still kind of sketchy:

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
#include <iostream>
#include <math.h>
#include <iomanip> 

using namespace std;
int const MAXSTUDENTS = 100;
int getStudentCount();
int noofstu;
int getExamScores();
int ExamScores[];
int num;
int getLabScores();
int LabScores[];
int i;






int getStudentCount()
{
	cout << "Calculating Students Grades" << endl;
	cout << "\nHow many students grades would you like to enter today?\n" << endl;
		cin >> noofstu;
		return 0;
}

int getExamScores()
{
	
	
	
	if (noofstu == 0)
	{
		cout << "You have entered no students, try again" << endl;
	}
	else 
	{ 
		for (num=0; num < noofstu; num++)
		{
			int n = num + 1;
	cout << "\nEnter Exam Score(s) for student "<< n++ << endl;
			cin >> ExamScores[num];
		}
		cout << "You've entered: ";
		for (num=0; num < noofstu; num++)
			cout << ExamScores[num]<< ", ";
		cout << "\n" << endl;

		int * Examscores;
	Examscores = new int;
	Examscores = &ExamScores[num];
	}
	return 0;
}

int getLabScores()
{



for (num =0; num < noofstu; num++)
{
	int n = num + 1;
	cout << "\nEnter Lab Score(s) for student "<< n++ << endl;
	cin >> LabScores[num];
}
cout<< "You Entered:";
for (num=0; num < noofstu; num++)
cout << LabScores[num]<< ", ";

int * Labscores;
Labscores = new int;
Labscores = &LabScores[num];


return 0;
}

int calculatePointGrades(Examscores, Labscores)
{

	int * Pointgrades;
	
	int sum = 0;

	for (int i=0; i<noofstu; i++)
	{
	 Pointgrades[i] = *Examscores[i]+ *Labscores[i]; 
	}
	return 0;
}


int main ()
{ //Open main
	

	getStudentCount();
	getExamScores();
	getLabScores();



	cin.ignore(2);
return 0;
} // Close main


First you say I need to declare Examscores and Labscores, and I think I understand why, but not how to go about doing it correctly. I understand when the return statement executes it deletes the pointer. What I don't know is where to declare the pointer again.
I think I need to declare:
getLabScores * Labscores = new &LabScores[num];
or just
* Labscores = new &LabScores[num];
or should I make the pointer a constant?

Whenever I declare them out of a function they get syntax errors until all that's left is the pointer = new. Kinda stumped.

you also say the three functions getExamScores(), getLabScores() and calculatePointGrades() should return a pointer to an int array, not an int. I think I took care of this because it was going to [noofstudents] orginally. Correct me if I'm wrong.
I think I'm understanding better but the execution is still off.

Also getting error C2448: 'calculatePointGrades' : function-style initializer appears to be a function definition. hmmmm
Last edited on
I'm also a C++ newbie but I'll try to help what I can.

"Also getting error C2448: 'calculatePointGrades' : function-style initializer appears to be a function definition."

You have a syntax error in your function definition. You need to specify the type of the parameters inside the parentheses in a function definition. Instead of

int calculatePointGrades(Examscores, Labscores)


It should be:

int calculatePointGrades(int Examscores, int Labscores)


Also, the variable names inside the parentheses don't need to be the same as the variables passed to it. For example, you could define your function as:

int calculatePointGrades(int My_ExamScores, int My_LabScores)
{
// code goes here
}


And then somewhere in your code, using the function like this would be fine:

calculatePointGrades (Examscores, Labscores);

I'm not sure what the advantages/disadvantages of doing this is, but for some reason I prefer to use different names for the parameters like this. Perhaps someone else can explain why this is a good/bad idea.




No I already had int before calcualatePointGrades. When I added int before the arguments I ran into another error having to do with pointers below. But as per helios, I don't think I should be declaring Examscores and Labscores in the argument.

Umm... I'm not sure which part of helios' comment you're referring to, but you *have* to specify the type of arguments to be passed in the function definition. Otherwise the compiler will think the calculatePointGrades doesn't take any arguments, and will output an error when you try to pass Examscores and Labscores to it. Are we talking about the same thing here?

I'm not sure I understand your problem statement either, but it might be possible to solve the problem without using any pointers.
Last edited on
Sorry, maybe I misread his comment but regardless here's what I got thus far:
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
#include <iostream>
#include <math.h>
#include <iomanip> 

using namespace std;
int const MAXSTUDENTS = 100;
int getStudentCount();
int noofstu;
int getExamScores();
int ExamScores[];
int num;
int getLabScores();
int LabScores[];
int i;
int calculatePointGrades(int *Examscores[],int *Labscores[]);










int getStudentCount()
{
	cout << "Calculating Students Grades" << endl;
	cout << "\nHow many students grades would you like to enter today?\n" << endl;
		cin >> noofstu;
		return 0;
}

int getExamScores()
{
	
	
	
	if (noofstu == 0)
	{
		cout << "You have entered no students, try again" << endl;
	}
	else 
	{ 
		for (num=0; num < noofstu; num++)
		{
			int n = num + 1;
	cout << "\nEnter Exam Score(s) for student "<< n++ << endl;
			cin >> ExamScores[num];
		}
		cout << "You've entered: ";
		for (num=0; num < noofstu; num++)
			cout << ExamScores[num]<< ", ";
		cout << "\n" << endl;

		int * Examscores;
	Examscores = new int;
	Examscores = &ExamScores[num];
	}
	return 0;
}

int getLabScores()
{



for (num =0; num < noofstu; num++)
{
	int n = num + 1;
	cout << "\nEnter Lab Score(s) for student "<< n++ << endl;
	cin >> LabScores[num];
}
cout<< "You Entered:";
for (num=0; num < noofstu; num++)
cout << LabScores[num]<< ", ";

int * Labscores;
Labscores = new int;
Labscores = &LabScores[num];


return 0;
}
	

int calculatePointGrades(int Labscores, int Examscores)
{
	
	

	int * Pointgrades;
	
	int sum = 0;

	for (int i=0; i<noofstu; i++)
	{
	 Pointgrades[i] = *Examscores[i]+ *Labscores[i]; 
	}
	return 0;
}


int main ()
{ //Open main
	

	getStudentCount();
	getExamScores();
	getLabScores();



	cin.ignore(2);
return 0;
} // Close main


and am getting errors:
error C2109: subscript requires array or pointer type
error C2109: subscript requires array or pointer type

Both for line 98

and this, besides the error, isn't even right, I have to modify the math a bit. I'm not adding them together exactly.

As far as I can tell it all leads back to the same two questions:
How do you assign an array to a pointer (because all efforts thus far keep popping up and giving me a syntax error or a linker error) ?

How do you call said array (or it's pointer) to a function?
Last edited on
Wow and to top it all off I now have an error: LNK2001 that I can't seem to get rid of, even after starting a new project, deleting all my old files etc....
It seems to be somehow attached to the code cause I've deleted what it specifies and it still comes back.

I get two of them one for:
int * Labscores;
and
int*Examscores;

Last edited on
About line 98: The error is saying that you can't apply the offset operator to things that aren't pointers. You're no longer passing pointers to the function. There are only two syntaxes for passing pointers: *p and p[] (and any combination of the two, such as **p, *p[], p[][]).
I myself pass pointers to arrays like this:
1
2
3
void f(void *p,int size){
    //do something
}

I the above example, I would access the elements of the array like this: *p or p[0], p[1], p[2], etc.

Now, there's something else that's bothering me here, and it's that you're not returning anything. I'll show you an example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
int *f(int *a,int size){
	//I'll skip all checks to make it simple.
	int *res=new int[size];
	*res=a[0];
	for (int i=1;i<size;i++)
		res[i]=res[i-1]+a[i];
	return res;
}

int main(){
	int size;
	//set size to something random
	int *v=new int[size];
	//v is filled
	int *newArray=f(v,size);
	//do some other thing
	delete[] v;
	delete[] newArray;
	return 0;
}


EDIT: LNK2001 occurs when you're referring to a symbol that doesn't exist. Usually happens due to typos or improper declarations.
Last edited on
so what should I return?
usually I just put in return 0 by default, and do everything i need to in the code itself.

EDIT:
Well I changed it to passing pointers the way you said and I still have no idea how to get rid of that LNK2001 error.
I can't see any typos or improper declarations, and I've looked over it quite a few times. Maybe I'm just tired, or ignorant of the problem, but I can't find the source of the problem. It says it's with "int * ExamScores" and "int * LabScores" but still I'm not sure, I'll keep looking but here's what I got, not much different then the code above:

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
#include <iostream>
#include <math.h>
#include <iomanip> 

using namespace std;
int const MAXSTUDENTS = 100;
int getStudentCount();
int noofstu;
int getExamScores();
int ExamScores[];
int num;
int getLabScores();
int LabScores[];
int i;
int calculatePointGrades(int *Examscores[],int *Labscores[]);










int getStudentCount()
{
	cout << "Calculating Students Grades" << endl;
	cout << "\nHow many students grades would you like to enter today?\n" << endl;
		cin >> noofstu;
		return 0;
}

int getExamScores()
{
	
	
	
	if (noofstu == 0)
	{
		cout << "You have entered no students, try again" << endl;
	}
	else 
	{ 
		for (num=0; num < noofstu; num++)
		{
			int n = num + 1;
	cout << "\nEnter Exam Score(s) for student "<< n++ << endl;
			cin >> ExamScores[num];
		}
		

	int * Examscores = 0;
	Examscores = new int;
	Examscores = &ExamScores[0];
	}
	return 0;
}

int getLabScores()
{



for (num =0; num < noofstu; num++)
{
	int n = num + 1;
	cout << "\nEnter Lab Score(s) for student "<< n++ << endl;
	cin >> LabScores[num];
}


int * Labscores = 0;
Labscores = new int;
Labscores = &LabScores[0];


return 0;
}
	

int calculatePointGrades(int Labscores[], int Examscores)
{
	
	

	int * Pointgrades;
	
	int sum = 0;

	for (int i=0; i<noofstu; i++)
	{
	 
	}
	return 0;
}


int main ()
{ //Open main
	

	getStudentCount();
	getExamScores();
	getLabScores();



	cin.ignore(2);
return 0;
} // Close main 



Thanks so much for the help btw, if I didn't have the help I'd be in the fetal position right now rocking back and forth.






Last edited on
Pages: 123