【解決方法】どうすれば…エラーなしでこれを解決できますか


#include <assert.h>
#include <ctype.h>
#include <limits.h>
#include <math.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char* readline();
char* ltrim(char*);
char* rtrim(char*);
char** split_string(char*);

int parse_int(char*);

/*
 * Complete the 'compareTriplets' function below.
 *
 * The function is expected to return an INTEGER_ARRAY.
 * The function accepts following parameters:
 *  1. INTEGER_ARRAY a
 *  2. INTEGER_ARRAY b
 */

/*
 * To return the integer array from the function, you should:
 *     - Store the size of the array to be returned in the result_count variable
 *     - Allocate the array statically or dynamically
 *
 * For example,
 * int* return_integer_array_using_static_allocation(int* result_count) {
 *     *result_count = 5;
 *
 *     static int a[5] = {1, 2, 3, 4, 5};
 *
 *     return a;
 * }
 *
 * int* return_integer_array_using_dynamic_allocation(int* result_count) {
 *     *result_count = 5;
 *
 *     int *a = malloc(5 * sizeof(int));
 *
 *     for (int i = 0; i < 5; i++) {
 *         *(a + i) = i + 1;
 *     }
 *
 *     return a;
 * }
 *
 */
int* compareTriplets(int a_count, int* a, int b_count, int* b, int* result_count) {

}

int main()
{
    FILE* fptr = fopen(getenv("OUTPUT_PATH"), "w");

    char** a_temp = split_string(rtrim(readline()));

    int* a = malloc(3 * sizeof(int));

    for (int i = 0; i < 3; i++) {
        int a_item = parse_int(*(a_temp + i));

        *(a + i) = a_item;
    }

    char** b_temp = split_string(rtrim(readline()));

    int* b = malloc(3 * sizeof(int));

    for (int i = 0; i < 3; i++) {
        int b_item = parse_int(*(b_temp + i));

        *(b + i) = b_item;
    }

    int resultcount;
    int* result = compareTriplets(3, a, 3, b, &result_count);

    for (int i = 0; i < result_count; i++) {
        fprintf(fptr, "%d", *(result + i));

        if (i != result_count - 1) {
            fprintf(fptr, " ");
        }
    }

    fprintf(fptr, "\n");

    fclose(fptr);

    return 0;
}

char* readline() {
    size_t alloc_length = 1024;
    size_t data_length = 0;

    char* data = malloc(alloc_length);

    while (true) {
        char* cursor = data + data_length;
        char* line = fgets(cursor, alloc_length - data_length, stdin);

        if (!line) {
            break;
        }

        data_length += strlen(cursor);

        if (data_length < alloc_length - 1 || data[data_length - 1] == '\n') {
            break;
        }

        alloc_length <<= 1;

        data = realloc(data, alloc_length);

        if (!data) {
            data = '\0';

            break;
        }
    }

    if (data[data_length - 1] == '\n') {
        data[data_length - 1] = '\0';

        data = realloc(data, data_length);

        if (!data) {
            data = '\0';
        }
    } else {
        data = realloc(data, data_length + 1);

        if (!data) {
            data = '\0';
        } else {
            data[data_length] = '\0';
        }
    }

    return data;
}

char* ltrim(char* str) {
    if (!str) {
        return '\0';
    }

    if (!*str) {
        return str;
    }

    while (*str != '\0' && isspace(*str)) {
        str++;
    }

    return str;
}

char* rtrim(char* str) {
    if (!str) {
        return '\0';
    }

    if (!*str) {
        return str;
    }

    char* end = str + strlen(str) - 1;

    while (end >= str && isspace(*end)) {
        end--;
    }

    *(end + 1) = '\0';

    return str;
}

char** split_string(char* str) {
    char** splits = NULL;
    char* token = strtok(str, " ");

    int spaces = 0;

    while (token) {
        splits = realloc(splits, sizeof(char*) * ++spaces);

        if (!splits) {
            return splits;
        }

        splits[spaces - 1] = token;

        token = strtok(NULL, " ");
    }

    return splits;
}

int parse_int(char* str) {
    char* endptr;
    int value = strtol(str, &endptr, 10);

    if (endptr == str || *endptr != '\0') {
        exit(EXIT_FAILURE);
    }

    return value;
}

私が試したこと:

このコードにはエラーの少ない解決策が必要でした

解決策 1

エラーメッセージを読んでください、それはかなり明確です。
あなたが使用しているコンパイラがわからないので、あなたのコードを オンライン GDB[^] そして、これで戻ってきました:

エラー
main.c: In function ‘main’:
main.c:84:48: error: ‘result_count’ undeclared (first use in this function); did you mean ‘resultcount’?
   84 |     int* result = compareTriplets(3, a, 3, b, &result_count);
      |                                                ^~~~~~~~~~~~
      |                                                resultcount
main.c:84:48: note: each undeclared identifier is reported only once for each function it appears in

したがって、エラーはファイル「main.c」の 84 行目、48 列目にあり、変数名を指しています。 result_count と言う "‘result_count’ undeclared (first use in this function); did you mean ‘resultcount’?"

これは非常に明快で、おそらく使用するつもりだった変数名も示唆しています!
上記の行を見ると、おそらく非常に正しいでしょう。

int resultcount;

そのため、用途に合わせて宣言を変更します。

int result_count;

そして、エラーは消えます。

コーディング中に毎日、おそらく 1 日に何度も構文エラーが発生することを予期する必要があります。経験の豊富さに関係なく、誰もがそうです。 変数やキーワードのスペルを間違えることがあります。 文字列やコード ブロックを閉じるのを忘れることがあります。 猫があなたのキーボードの上を歩いて、とても奇妙なことをタイプすることがあります。 メソッド呼び出しに必要なパラメーターの数を忘れてしまうことがあります。

我々はすべての間違いを犯します。

そして、私たちは皆そうしているので、構文エラーを修正する必要があります。他の人が修正してくれるのを待つよりも、方法を学んで自分で修正する方がはるかに迅速です! したがって、エラー メッセージの読み方と、コンパイラが間違っていると言っていることに照らして記述されたコードを解釈する方法を学ぶことに少し時間を費やしてください。

だからこれを読んでください: 問題を解決するコードの書き方、初心者向けガイド パート 2: 構文エラー[^] – 次回コンパイル エラーが発生したときに役立つはずです。

コメント

タイトルとURLをコピーしました