Run time failure 3??????

Why do i get a run time check failure 3 here?

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
int* sum(int n[5]);

int main()
{

	
	int n[5] = { 2,3,4,5,6 };
	int *ptr1;
	ptr1 = sum(n);

	for (int i = 0; i < 5; i++)
	{
		cout << ptr1[i] << " ";
	}
	cout << endl;



	return 0;
}

int* sum(int n[5])
{    
	
	int *ptr;

	for (int i = 0; i < 5; i++)
	{
		ptr[i] = pow(n[i],2);
	}

	return ptr;

}[code]
[/code]
Last edited on
Line 25: pointer is an uninitialized pointer. It contains garbage.

Line 29: You're trying to index an uninitialized pointer. Pretty much guaranteed to crash your program.

PLEASE USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.



thx i just had to initialize ptr with n :D i didn't know about the format option.
Topic archived. No new replies allowed.