簡體   English   中英

錯誤:*(int **,int,int,int)'的間接級別與'int()'不同,如何解決我的情況?

[英]Error: *(int **,int,int,int)' differs in levels of indirection from 'int ()', How can I fix it in my case?

我正在嘗試解決我遇到的一項家庭作業問題。 我應該創建一個鏈表和一個數組。 鏈表的元素是三個整數的結構以及指向下一個節點的指針。 數組的元素也是三個整數的結構。 將這些特定結構放入鏈接列表&數組的條件是,當給定矩陣滿足時:i + j等於特定迭代中矩陣中的元素(其中i和j是迭代元素)。

 Severity Code Description Project File Line Suppression State Error C2040 'createThreeArr': 'threesome *(int **,int,int,int)' differs in levels of indirection from 'int ()' 

我在這篇文章的標題中看到了此錯誤,有人可以幫助我理解為什么會這樣嗎? 我做錯了什么?

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

typedef struct threesome
{
    int sum;
    int i;
    int j;
}threesome;

typedef struct list
{
    threesome data;
    struct list* next;
}list;


int createArrayAndList(int** arr, int rows, int cols, threesome** resArr, list** resList)
{
    int num;
    *resList = createThreeList(arr, rows, cols, &num);
    *resArr = createThreeArr(arr, rows, cols, num);
    return num;
}
threesome* createThreeArr(int** arr, int rows, int cols, int num) {
    threesome* new_array;
    int i, j, k;
    if (!num) return NULL;
    new_array = (threesome*)malloc(sizeof(threesome)*num);
    k = 0;
    for (i = 0; i < rows; i++)
        for (j = 0; j < cols; j++)
            if (arr[i][j] == i + j) {
                new_array[k].sum = arr[i][j];
                new_array[k].i = i;
                new_array[k].j = j;
                k++;
            }
    return new_array;
}
list* createThreeList(int** arr, int rows, int cols, int* length_list) {
    int i, j, count = 0;
    list *head = NULL, *temp1 = NULL, *temp2 = NULL;
    for (i = 0; i < rows; i++)
        for (j = 0; j < cols; j++) {
            if (arr[i][j] == (i + j)) {
                temp2 = (list*)malloc(sizeof(list));
                temp2->data = createThree(i + j, i, j);
                temp2->next = NULL;
                if (!count) {
                    head = temp2;
                    temp1 = head;
                    count++;
                    continue;
                }
                else {
                    temp1->next = temp2;
                    temp1 = temp1->next;
                    count++;
                    continue;
                }
            }
        }
    *length_list = count;
    return head;
}
threesome createThree(int num, int i, int j) {
    threesome strc;
    strc.sum = num;
    strc.i = i;
    strc.j = j;
    return strc;
}

你有:

int createArrayAndList(int** arr, int rows, int cols, threesome** resArr, list** resList)
{
    int num;
    *resList = createThreeList(arr, rows, cols, &num);
    *resArr = createThreeArr(arr, rows, cols, num);
    return num;
}
threesome* createThreeArr(int** arr, int rows, int cols, int num) {
    …

要將呼叫createThreeArr()createArrayAndList()隱式聲明createThreeArr()和調用createThreeList()做大致相同):

extern int createThreeList();
extern int createThreeArr();

這些是返回帶有未定義(但不是可變參數)參數列表的int函數。

當編譯器遇到threesome* createThreeArr(int** arr, int rows, int cols, int num) {它以為那不是您之前所說的! 並生成錯誤消息。

在調用它們之前聲明或定義您的函數。 C99需要它。 如果這樣做,您會得到更好的錯誤檢查。 您可以在使用static函數之前先定義它,而無需預先聲明它。

static threesome *createThreeArr(int **arr, int rows, int cols, int num);
static list *createThreeList(int **arr, int rows, int cols, int *length_list);
static threesome createThree(int num, int i, int j);
static int createArrayAndList(int** arr, int rows, int cols, threesome** resArr, list** resList);

static int createArrayAndList(int **arr, int rows, int cols, threesome **resArr, list **resList)
{
    …
}

…

如果是我的代碼,除main()之外的所有功能都是static (如上所述); 如果它們在此源文件之外可見,則將在標頭中聲明它們,標頭將包含在此處以及使用函數的其他位置。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM