I am doing malloc(0) and then doing strcpy and then reversing, and its working, why??
and if i dont do malloc(0) and then try to strcpy program carashed as expected, but how does malloc(0) making a difference.
#include <stdio.h>
char* reverse(char *data);
void my_Strcpy(char* dest,char* source);
main()
{
char* p_Name = "Mithun P";
char a_Name[] = "Mithun P";
char *pd_Name = malloc(0);//what is happening here
my_Strcpy(pd_Name,"Mithun P");
//printf("reverse of p_Name is %s \n",reverse(p_Name));
printf("reverse of a_Name is %s \n",reverse(a_Name));
printf("reverse of pd_Name is %s \n",reverse(pd_Name));
getchar();
}
void my_Strcpy(char* dest,char* source)
{
while(*dest++ = *source++);
}
char* reverse(char * data)
{
int size = 0;
int i,j;
char* temp = data;
while(*temp++)
size++;
printf("size is %d\n",size);
for(i = 0, j = size-1;i < size/2; i++ , j--)
{
char temp = data[i];
data[i] = data[j];
data[j] = temp;
}
return data;
}