#include "iostream"
usingnamespace std;
char *value = "Sandeep";
char* delValue(char* p_value,char alphabet)
{
int placeholder = 0;
int count = 0;
while(p_value[count] != NULL)
{
if(placeholder)
{
p_value[count] = p_value[(count+placeholder)];
// Why we get exception here. this seems to be valid line
}
if(p_value[count]== alphabet)
{
placeholder++;
}else{
count++;
}
}
return p_value;
}
int main()
{
cout<<delValue(value,'e');
return 0;
}
I am trying to remove 'e' from "Sandeep" in above program. But compiler is throwing un-handled exception at line p_value[count] = p_value[(count+placeholder)];
can any one tell me why i got exception(logic might be wrong, but operation is valid).
At some point count+placeholder is greater than the size of the array, meaning you're trying to access memory that doesn't exist.
Vlad had the right answer
No you are wrong. Even you declare the pointer without const in any case you may not change string literals. The behaviour is undefined. So your code is principally incorrect.