Let's say i have this program:
#include <iostream>
using namespace std;
int main(){
char *c = new char;
cin>>c;
c = "Hello";
cin>>c;
delete c;
}
Now, I understand that char pointer is a constant pointer who'se values must not be changed ( or else it's undefined behavior ) , however , in the following program I don't understand really what's going on , from what I do all I can see is that I'm trying to create constant string literals somewhere in memory and address them to the pointer ( hence the cin>>c;
and c = "Hello";
) which is allowed with char pointer ( I'm not trying to change the values pointed to by the pointer ) , however , perhaps there's some sugar-code in the program above or I'm just interpreting it wrong, and that's why I need help, why does the above program throws me an error indicating about undefined behavior? , Also , since i'm inputting more than 1 literals into c
, am I supposed to do : delete[] c
? (since the collection of literals is basically an array, although c
is not declared as an array of char pointers but a pointer to char )