동적으로 할당 된 2 차원 배열을 성공없이 qsort하려고합니다. 전역 배열 (예 : fileList [148096] [100])을 사용하면 문제가 없으므로 동적 할당과 관련이 있다고 가정합니다. 어떤 아이디어?동적으로 할당 된 2 차원 문자 배열을 할당하는 방법
// global:
char **fileList;
void allocateFileListArray(void)
{
int arraySize = 148096;
int fileNameLength = 100;
/* allocate storage for an array of pointers */
fileList = (char **) malloc(arraySize * sizeof(char *));
/* for each pointer, allocate storage for an array of chars */
for (int i = 0; i < arraySize; i++)
{
if ((fileList[i] = (char *) malloc(fileNameLength * sizeof(char))) == NULL)
printf("failed fileList alloc\n");
}
}
void sortTheArray(int fileListCount)
{
qsort ((char *) fileList, fileListCount, sizeof (*fileList),
(compfn) compareStrings);
}
int compareStrings(char *stackA, char *stackB)
{
int result;
result = strcmp(stackA->name, stackB->name);
return(result);
}
2D 배열을 정렬하는 정의는 무엇입니까? 당신은 아마도 행을 정렬하고 싶습니다 ... 그 다음에 적절한 compare 함수를 전달하십시오 ... –
'fileListIndex'를 선언하지 않았습니다. 그냥 짐작 하긴하지만, 고정 크기 배열 유형에는 맞지만 동적으로 할당 된 배열에는 맞지 않는'(sizeof (fileList)/sizeof (* fileList)) '로 계산하고 있습니다. –
정확히 무엇이 문제입니까? BTW compareStrings()에 대한 정의를 추가하십시오. 또한 : 단편에있는 캐스트 중 어느 것도 필요하지 않습니다. 그들을 버려. – wildplasser