Possible Duplicate:
Can a local variable’s memory be accessed outside its scope?
So heres a simple c++ function. What it does it take an array of characters as its argument and a integer n and then creates a new character array with only n elements of the array.
char * cutString(char * ch , int n){
char * p = new char[n];
int i ;
for(i = 0 ; i < n ; i++)
p[i] = ch[i];
while(i <= n ){
p[i++] = '\0';
}
return p ;
}
This works just fine but if I change char * p = new char[n];
to char p[n];
I see funny characters. What happens? What difference does the former make? Also, p is a temporary variable; how does the function return it successfully?