繁体   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