I have defined 2 variables, one a pointer and one an array
char* ptr;
char* array;
ptr = "12345";
array = new int[5];
for(int i = 0; i < 5; i++)
array[i] = i;
while(*ptr != 0)
cout << *ptr++ << endl;
//Get garbage values
for(int i = 0; i < 5; i++)
cout << ptr[i];
I was wondering what are the major differences between the variables. And why I get garbage values when I try to print the values in "ptr[]" the array way, but it's perfectly fine when iterate through the vales. I can't seem to understand how my variable "ptr" can point to 5 characters, since it should only be able to point to one.
ptr++changes the value ofptr. You never reset it.