Help assignment due tonight!

I'm having some trouble with pointers and dynamic memory allocation.
When I try to compile this code I get several errors. It says that its an invalid conversion from int* to int on the line when I try to assign the random integer to the array, and it says the same thing on the line when I try to run the function in main. I'm not sure why this is happening and I really need help. Thanks in advance!

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
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

int getRandomInteger(void)
{
	int number;
	srand(time(0));
	number = rand() % 1000 + 1;
	return number;
}


int* createArray(int a)
{
	int *p = new int[a];
	for(int i = 0; i < a; i++)
	{
		p[i] = 5;
	}
	for(int j = 0; j < a; j++)
	{
		p[j] = getRandomInteger;
	}
	return p;
}




int main()
{
	int input;
	cout << "Enter an integer: ";
	cin >> input;
	int *ptr;
	*ptr = createArray(input);
}
On line 24 you are trying to store a pointer to function into an int variable. Perhaps you should call some function?


On line 38 the function call returns a pointer. That you try to store into the int object that is at location specified by pointer ptr. (Alas, the ptr is uninitialized and does not point to any valid int.) Pointers are stored in pointers, not in ints.
Topic archived. No new replies allowed.