Consider the following code:
#include<stdio.h>
#define SET_BIT(buf,n,val)buf[n]=val
int main()
{
char ch='D';
char *name="Yankee Duddle";
int i=5;
SET_BIT(name,i,ch);
printf("%s",name);
}
When compiling with Borland C it worked fine. However when I tried cimpiling it with VS 2005 I got the following error:
"Unhandled exception at 0x00413529 in Learning C.exe: 0xC0000005: Access violation writing location 0x00415645."
When I changed the code to:
char name[]="Yankee Duddle";
Instead of
char *name="Yankee Duddle";
It worked fine. I found this resolution in a forum.
But based on what I learnt in C I don't understand why it did not work in VS. And why by making it an array rather than a pointer it made a difference?