How come that output of following programm is "pankaj", I was expecting a compilation error like "can not modify a constant string"
#include<iostream>
using namespace std;
void fun(const char *a)
{
a = "pankaj";
cout << a;
}
int main()
{
const char *ptr = "GeeksforGeeks";
fun(ptr);
return 0;
}
You´re not modifying any string, just the parameter a
.
First, it´s a pointer to the passed value, then it´s a pointer to a string constant in your program.
You can´t change the passed string data, but changing the pointer is fine.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments