一个C语言指针数组问题。求大佬解答?
#include<stdio.h>
#include<string.h>
#include<malloc.h>
void stringinput(int N, char *s[]) { //字符串输入函数
int i;
char s_tem[80]; //用于临时接收输入的字符串
for (i = 0; i < N; i++) {
scanf("%s", s_tem); //这个遇到空格停止,输入整行用gets(s_tem);
s[i] = (char*)malloc((strlen(s_tem) + 1) * sizeof(char)); //给字符串分配空间
strcpy(s[i], s_tem);
}
}
void stringoutput(int N, char *s[]) {
while (N-- > 0)puts(*s++);
}
void stringsort(int N, char *s[]) { //使用归并排序算法
int w, l, m, r, i, j, k;
char **s_tem = (char**)malloc(N * sizeof(char*)); //用于存放迭代的字符串
for (w = 1; w < N; w <<= 1) {
//w是分割的最小有序区域的宽度
for (l = k = 0; l < N; l = r) {
//l是被归并的靠左的有序区间的左端
m = l + w; //m是归并的靠右的有序区间左端
r = m + w; //r是下一个l
//如果只剩下一个有序区域,则直接复制
if (m >= N)for (i = l; i < N; i++)s_tem[i] = s[i];
else {
//否则进行归并
if (r > N)r = N;
i = l, j = m;
while (i < m && j < r) {
if (strcmp(s[i], s[j]) < 0)s_tem[k++] = s[i++];
else s_tem[k++] = s[j++];
}
while (i < m)s_tem[k++] = s[i++];
while (j < r)s_tem[k++] = s[j++];
}
}
//将迭代后的字符串数组赋值回s
for (i = 0; i < N; i++)s[i] = s_tem[i];
}
free(s_tem);
}
int main() {
int N;
scanf("%d", &N);
char **s = (char**)malloc(N * sizeof(char*));
stringinput(N, s);
stringsort(N, s);
stringoutput(N,s);
while (N-- > 0)free(s[N]);
free(s);
return 0;
}