Проблема несовместимого типа указателя при копировании строки и печати в файл - PullRequest
0 голосов
/ 05 января 2019

Я получаю два предупреждения одного типа с этим кодом, компилируемым в GCC.

предупреждение: несовместимые типы указателей передают 'char * [1001]' в параметр

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

    char line[1001]; // The line supports up to a 1000 characters
    char *lines[11][1001]; // An array of lines (up to 10 lines where each line is a 1000 characters max)

    int main(){

        int 
        i, // Line number
        j; // Length of the line
        char result[100], text[100];
        FILE *file;

        strcpy(text, "String No."); // The default text

        file = fopen("test.txt", "w+"); // Open the file for reading and writing

        for(i = 0; i < 10; i++){ // Loop to create a line.

            if(i != 9){ // If the line is NOT at the 10th string

                sprintf(result, "%s%d, ", text, i); // Format the text and store it in result

            }
            else{

                sprintf(result, "%s%d ", text, i); // Format the text and store it in result            

            }

            printf("%s", result); // Display the result variable to the screen

            strncat(line, result, 15); // Concatenate all strings in one line

        }

        strncat(line, "\n\n", 2); // Add a new-line character at the end of each line

        for(j = 0; j < 10; j++){ // Now loop to change the line.

            strcpy(lines[i], line); // Copy the line of text into each line of the array

            fputs(lines[i], file); // Put each line into the file        

        }

        fclose(file);  

    }

Судя по всему, это исправление должно быть довольно простым, просто я еще не думал об этом или не пришел к нему. Спасибо!

1 Ответ

0 голосов
/ 05 января 2019

относительно:

char *lines[11][1001]; 

это огромный массив указателей, а не массив из одиннадцати массивов из 1001 символа. Предложить:

char lines[11][1001]; 

Кстати: почему 11 массивов символов, когда используются только 10?

КСТАТИ: почему 1001 символов, когда максимальный 15 * 10 = 150 символов?

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...