这里为什么打印NULL? - C-90

Why is NULL printed here? - C-90

代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define N 500
#define M 100

/*Version 1*/
void replace_word(char *word);
void add_new_word_dictionary(void);
void do_nothing(void);
void return_basic(void);
void check_word(void);
void compute_words(void);
void compute_characters(void);
void compute_ch_sp(void);
void compute_num_dif_words(void);
void create_istogram(void);
void save_file(void);
int get_choice(void);
void return_word(void);
void insert_text(int numwords, char matrix[N][M], int posit);

int main() {
    int j;
    int choice = 0;
    char matrix[N][M];
    char word[40] = { "t" };
    while (1) {
        choice = get_choice();
        if (choice == 0) {
            insert_text(M, matrix, 1);
        }
        if (choice == 1) {
            add_new_word_dictionary();
        }
        if (choice == 2) {
            do_nothing();
        }
        if (choice == 3) {
            save_file();
        }
        if (choice == 4) {
            compute_words();
        }
        if (choice == 5) {
            break;
        }
        for (j = 0; j < M; j++) {
            printf("%s", matrix[N][M]);
        }
    }

    printf("\n End of Program \n");
    return 0;
}

void replace_word(char *word) {
    return;
}

void add_new_word_dictionary(void) {
    char word[50] = { "s" };
    printf("\nPlease enter the word\n");
    scanf("\n%s", word);
    printf("Your word is %s", word);
    return;
}

void do_nothing(void) {
    printf("\n do_nothing \n");
    return;
}

void return_basic(void) {
    printf("\n return basic \n");
    return;
}

void check_word(void) {
    printf("\n check word \n");
    return;
}

void compute_words(void) {
    printf("\n compute_words \n");
    return;
}

void compute_characters(void) {
    printf("\n compute characters \n");
}

void compute_ch_sp(void) {
    printf("\n compute_ch_sp \n");
    return;
}

void compute_num_dif_words(void) {
    printf("\n compute_num_same_words \n");
    return;
}

void create_istogram(void) {
    printf("\n create istogram \n");
    return;
}

void save_file(void) {
    printf("\n save_file \n");
    return;
}

int get_choice(void) {
    int choice = 0;
    printf("\n Select a choice from  the below \n");
    printf("\n Select 0 to add text \n");
    printf("\n Select 1 to add new words in the dictionary \n");
    printf("\n Select 2 to enter  enter correction mode \n");
    printf("\n Select 3 to save the text \n");
    printf("\n Select 4 to see the statistics about your text \n");
    printf("\n Select 5 to exit the program\n");
    scanf("\n%d", &choice);
    return choice;
}

void insert_text(int numwords, char matrix[N][M], int  posit) {
    int i;
    int j;
    char word2[40] = { "" };

    while (strcmp(word2, "*T*E*L*O*S*")) {
        printf("\n Add the word \n");
        scanf("\n%s", word2);

        if (posit + 1 > numwords) {
            printf("\n Out of Bounds \n ");
        }

        for (i = numwords - 2; i >= posit; i--) {
            strcpy(matrix[i + 1], matrix[i]);
            if (!i)
                break;
        }
        strcpy(matrix[posit], word2);
        j++;
    }
    for (i = 0; i < j; i++) {
        printf("%s", matrix[i]);
        printf("\n j is %d\n", j);
    }
    return;
}

问题:我有一个名为 insert_text 的函数。此函数在数组的第一个位置添加一个字符串(至少我认为它是这样做的),如果 choice 为 0 则调用它并自行执行直到给出字符串 *ΤELOS*。当在 insert_text 中打印 matrix 时,我得到一堆 *(null)*s... 我可以计算 matrix 有多少个单词(通过声明变量 j并在 while 循环中递增 1,但这似乎也不起作用。我该如何解决这个问题?

打印代码不正确:matrix 是一个包含 M 个字符的 N 个数组的数组,您在其中存储以空字符结尾的 C 字符串。按照编码,您将刚好超出数组末尾的单个字符传递给 printf for %s,它需要一个字符串。循环应该是:

    for (j = 0; j < N; j++) {
        printf("%s ", matrix[j]);
    }

注意 char matrix[N][M]; 是未初始化的,所以它的内容看起来是随机的。将 matrix 初始化为 char matrix[N][M] = { "" };

另请注意,在 add_new_word_dictionary() 中,scanf() 转换应为 scanf("%49s", word);,以防止在用户输入很长的单词时可能出现缓冲区溢出。

insert_text 相同,代码应为 scanf("%39s", word2),您应测试 return 值以检查输入错误。

最后,C中数组是从0开始索引的,所以insert_text的位置应该是0,字数应该是N,而不是M.

测试和插入循环也有问题

这是修改后的版本:

// call from main as insert_text(N, matrix, 0);
//
void insert_text(int numwords, char matrix[N][M], int posit) {
    char word2[40];
    int i, j = 0;

    for (;;) {
        printf("\n Add the word \n");
        if (scanf("%39s", word2) != 1) {
            break;  // end of file or input error
        }
        if (!strcmp(word2, "TELOS")) {
            break;  // magic word
        }
        if (posit >= numwords) {
            printf("\n Out of Bounds \n");
            break;
        }
        for (i = numwords - 2; i >= posit; i--) {
            strcpy(matrix[i + 1], matrix[i]);
        }
        strcpy(matrix[posit], word2);
        j++;
    }
    for (i = 0; i < j; i++) {
        printf("%s ", matrix[i]);
    }
    printf("\n j is %d\n", j);
}

这是整个程序的修改版本:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define N 500
#define M 100

/*Version 1*/
void replace_word(char *word);
void add_new_word_dictionary(void);
void do_nothing(void);
void return_basic(void);
void check_word(void);
void compute_words(void);
void compute_characters(void);
void compute_ch_sp(void);
void compute_num_dif_words(void);
void create_istogram(void);
void save_file(void);
int get_choice(void);
void return_word(void);
void insert_text(int numwords, char matrix[N][M], int  posit);

int main() {
    int j, done = 0;
    char matrix[N][M] = { "" };

    while (!done) {
        switch (get_choice()) {
        case 0:
            insert_text(N, matrix, 0);
            break;
        case 1:
            add_new_word_dictionary();
            break;
        case 2:
            do_nothing();
            break;
        case 3:
            save_file();
            break;
        case 4:
            compute_words();
            break;
        default:
            done = 1;
            break;
        }
        for (j = 0; j < N; j++) {
            if (matrix[j][0])
                printf("%s ", matrix[j]);
        }
        printf("\n");
    }
    printf("\n End of Program \n");
    return 0;
}

void add_new_word_dictionary(void) {
    char word[50];
    printf("\nPlease enter the word\n");
    if (scanf("%49s", word) == 1)
        printf("Your word is %s\n", word);
}

void replace_word(char *word)   { printf("\n replace word \n"); }
void do_nothing(void)           { printf("\n do_nothing \n"); }
void return_basic(void)         { printf("\n return basic \n"); }
void check_word(void)           { printf("\n check word \n"); }
void compute_words(void)        { printf("\n compute_words \n"); }
void compute_characters(void)   { printf("\n compute characters \n"); }
void compute_ch_sp(void)        { printf("\n compute_ch_sp \n"); }
void compute_num_dif_words(void) { printf("\n compute_num_same_words \n"); }
void create_istogram(void)      { printf("\n create istogram \n"); }
void save_file(void)            { printf("\n save_file \n"); }

int get_choice(void) {
    int choice = -1;
    printf("\nSelect a choice from  the below \n");
    printf("Select 0 to add text \n");
    printf("Select 1 to add new words in the dictionary \n");
    printf("Select 2 to enter  enter correction mode \n");
    printf("Select 3 to save the text \n");
    printf("Select 4 to see the statistics about your text \n");
    printf("Select 5 to exit the program\n");
    scanf("%d", &choice);
    return choice;
}

// call from main as insert_text(N, matrix, 0);
//
void insert_text(int numwords, char matrix[N][M], int posit) {
    char word2[40];
    int i;

    for (;;) {
        printf("\n Add the word \n");
        if (scanf("%39s", word2) != 1) {
            break;  // end of file or input error
        }
        if (!strcmp(word2, "TELOS")) {
            break;  // magic word
        }
        if (posit >= numwords) {
            printf("\n Out of Bounds \n");
            break;
        }
        for (i = numwords - 2; i >= posit; i--) {
            strcpy(matrix[i + 1], matrix[i]);
        }
        strcpy(matrix[posit], word2);
        posit++;
    }
    for (i = 0; i < posit; i++) {
        printf("%s ", matrix[i]);
    }
    printf("\n posit is %d\n", posit);
}