Hi.Just started using pointers. I'm trying to use them instead of arrays but i get this error after the program finishes: Run-Time Check Failure #2 - Stack around the variable 'q' was corrupted. Anyway here's the code:
1 2 3 4 5 6 7 8 9 10 11 12 13
#include <iostream>
usingnamespace std;
int main ()
{int q=0,n,i,aux,j; int* p;
cout<<"Introduceti numarul de elemente:"; cin>>n;
p=&q;
for (i=1; i<=n; i++) cin>>*(p+i);
for (i=1; i<n; i++) for (j=i+1; j<=n; j++) if (*(p+i)>*(p+j)) {aux=*(p+i);
*(p+i)=*(p+j);
*(p+j)=aux;}
for (i=1; i<=n; i++) cout<<*(p+i)<<" ";
return 0;
}
I'm trying to do this because with a pointer i can have an infinite number of spaces, also i use just as much memory as i need. With an array i got an error that its too long after trying to get more then 1010 spaces. Is there any way i can use a pointer this way?
@Skillless
I needed that number of spaces for solving a problem at a contest. Anyway i didn't realized the size of memory it requires. So thx for pointing that out.
@harsh
is there any way i create a memory location each time i introduce a value? So that there always is a free memory location after the last introduced value?