字符串选择阵列排序(c)



i编译下面的C代码,我发现没有错误,但是当我运行它时,程序将停止并获得此错误:分段故障(core dustped)。我尝试对任何行中有一个字符串的数组进行选择。任何

#include <stdio.h>
#include <string.h>
#define n 50
int main() {
    int i, j, pl;  /* pl = number of words */ 
    char words[pl][n];
    char *temp;
    scanf("%d", &pl);
    for (i = 0; i < pl; i++); {
        scanf("%s", words[i]);
    }
    /* bla bla
       bla bla  */
    for (i = 0; i < (pl - 1); i++) {
        pos = i;
        for (j = i + 1; j < pl; j++) {
            if (strcmp(words[pos], words[j]) >= 0)
                pos = j;
        }
        if (pos != i) {
            strcpy(temp, words[i]);
            strcpy(words[i], words[pos]);
            strcpy(words[pos], temp);
        }
    }
for(i=0; i<pl; i++);

请注意,Semicolon,以下块只能运行一次。因此,您的"单词"无法正确初始化。

scanf("%d",&pl);
char words[pl][n];//put after the pl is determined.

最新更新