Code: #include<iostream> #include<conio.h> #include<string.h> using namespace std; void main() { char *a; int i,count=0; a="manu"; cout<<"Enter the string:"; cin>>a; for(i=0;a[i]!='\0';++i) { ++count; } cout<<"The length of the string is %d"<<count; } The following program was tried by me on Visual Studio 2008 and I got an error :- "Unhandled exception at 0x689e63b1 (msvcp90d.dll) in ptr4.exe: 0xC0000005: Access violation writing location 0x00f17840." However turbo c(which i have been using for long and plan to migrate to vs2008) shows no error Please help me PS The code is nt optimized
The error came because 'a' is pointing to const "manu". that means if manu address is starting at "0x1000" then 'a' will also point to "0x1000". when we try to enter our own string and tries to update the 'a'. it leads to an error. to solve this problem, we need to allocate memory and copy the string "manu" to 'a' instead of assigning.