Help with function/array/pointers?

I'm having some trouble using pointers to make an array with an unknown length and then passing that to a function. Here is my code.

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
#include <iostream>

using namespace std;

// FUNCTION
int nextVisitor( int x, bool stalls[])
	{
	int i;
	int count = 0;
	int finalcount = 0;
	int place = 0;

	for ( i = 0; i <=(x-1); i++)
		stalls[i] = false;

	i = 0;

	while (i <= (x-1))
	{
		if (stalls[i] == false)
		{
			count++;
			i++;

		}
		else
		{
			if (count > finalcount)
			{
				place = i;

			}
		}	
	}

	count = count /2;
	place = place + count;

	stalls[place] = true; // visitor goes here

	// print array
	for (i =0; i <= 9; i++)
		cout << stalls[i];
	//print array

	return 0;
	}
//FUNCTION END

int main()
{
	int length;

	cout << "How many stalls are there?" << endl;
	cin >> length;

    bool *stArray; 
	*stArray = new bool[length];

	nextVisitor(length, *stArray);

    delete[] stArray;

    return 0;

}


here are the errors I get, around line 60


1>------ Build started: Project: project3, Configuration: Debug Win32 ------
1> project3.cpp
1>c:\users\jam\documents\visual studio 2010\projects\project3\project3\project3.cpp(58): warning C4800: 'bool *' : forcing value to bool 'true' or 'false' (performance warning)
1>c:\users\jam\documents\visual studio 2010\projects\project3\project3\project3.cpp(60): error C2664: 'nextVisitor' : cannot convert parameter 2 from 'bool' to 'bool []'
1> Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
nextVisitor(length, *stArray);

*stArray is the same as stArray[0]. That is, you're only passing the first element.

If you want to pass the array, lose the *:

nextVisitor(length, stArray);
Oh! Thank you Disch! That solved the problem!

I can now run the program but I get a runtime error after the amount of stalls is input saying stArray isn't initialized. What would I add to initialize it? Thsi is my first program pulling from the heap so I'm struggling with some of the details.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
	int length;
	int j;

	cout << "How many stalls are there?" << endl;
	cin >> length;

    bool *stArray; 
	*stArray = new bool[length];

	for ( j = 0; j <=(length-1); j++)
	stArray[j] = false;

	nextVisitor(length, stArray);

    delete[] stArray;
    return 0;
*stArray = new bool[length];

stArray is a pointer. So the * in *stArray is you trying to dereference a pointer. It's the same as doing this:

stArray[0] = new bool[length];

Which of course doesn't work, because stArray is not an array yet, it's just a bad pointer.

You want to assign the pointer not the data that it points to. So again, lose the *:

stArray = new bool[length];


The compiler really should have caught this and given you a compiler error. There's a clear type mismatch here. What compiler are you using?
Last edited on
Topic archived. No new replies allowed.