I am writing a small program to order a matrix of strings. In the ordination algorithm, I use this function to replace two lines, but in the same cases, the program is split into Segmentation Fault Error. I realized this because of how I initialized temp string, but I did not understand 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 to me why he is giving this error? And is there any other way to do this correctly without using dynamic allocation, which I don't really know? Thank you very much!
Vitto source
share