how to recycle pointer

The compiler complains about redeclaration of n_ptr, which makes sense. My goal though was to recycle the pointer and use n_ptr again. I need to know how to do this without memory leaks/side effects. Do I delete the pointer?

snippet
1
2
3
4
5
6
7
8
int * n_ptr = x_array;
...other stuff
int y_array[30];
int * n_ptr = y_array;
for(int i = 0; i<30; ++i)
	{
	int * n_ptr = i * 5;
	}
Last edited on
The complaint is that you're trying to create a whole new object with the same name as an already existing one. Don't create a new pointer. Just reuse the existing one. Line 4 should be: n_ptr = y_array; Line 7 should be *n_ptr = i * 5; (although line 7 makes no sense; I assume you've just not finished it yet).
Last edited on
No need to redeclare n_ptr as it is already type * pointer.

1
2
3
4
5
6
7
8
int * n_ptr = x_array;
...other stuff
int y_array[30];
n_ptr = y_array;  // re-use the same one
for(int i = 0; i<30; ++i)
	{
	*n_ptr = i * 5; // change value
	}


Thank you
Last edited on
Topic archived. No new replies allowed.