I'm trying to write a C code to sort strings, but there always shows an error message in line 13.
#include <stdio.h>
#include <string.h>
void SortString(char *strings[], int size)
{
char temp[10];
for(int i =0; i < size -1; i++)
for(int j = i+1; j<size; j++)
{
if (strcmp(strings[i], strings[j])>0)
{
strcpy(temp, strings[i]);
strcpy(strings[i], strings[j]); //Error: Thread 1: EXC_BAD_ACCESS (code=2, address=0x100000fa6)
strcpy(strings[j], temp);
}
} }
int main(){
char *names[] = {"D", "C", "B", "A"};
SortString(names, 4); }
I know I can change *name[]
into name[][20]
and change void SortString(char *strings[], int size)
to void SortString(char strings[][20], int size)
to make the code correct, but why *name[] is wrong?
转载于:https://stackoverflow.com/questions/53057327/im-trying-to-write-a-c-code-to-sort-string-but-there-always-shows-an-error-mes