I'm writing a little program to order a matrix of strings. In the ordination algorithm I use this function to swap two strings, but in same cases the program crashhes with Segmentation Fault Error
. I have understood it's caused by how I inizialize the temp string
, but I haven't understood why.
void stringSwap(char*string1,char*string2)
{ const int dim=sizeof(string1);
char temp[dim];
strcpy(temp,string1);
strcpy(string1,string2);
strcpy(string2,temp);
}
Can someone explain me why it gives this error? And is there another mode to do this thing correctly without using the dinamic allocation that I don't know actually? Thank you very much!