简体   繁体   中英

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

I am trying to solve one of my homework problems that I was given. I am supposed to create a linked list and an array. The elements of the linked list is a struct of three ints and a pointer to the next node. The elements of the array are structs of three ints as well. A condition to put these specific struct into the linked list& array is when a given matrix fulfills: i+j equals to the element in the matrix in a specific iteration (where i and j are the iteration elements).

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

I got this error shown in the title of this post, can anyone help me understand why is this happening? What have I done wrong?

#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;
}

You have:

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) {
    …

The call to createThreeArr() in createArrayAndList() implicitly declared createThreeArr() (and the call to createThreeList() does roughly the same):

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

Those are functions returning an int with undefined (but not variadic) argument lists.

When the compiler comes across threesome* createThreeArr(int** arr, int rows, int cols, int num) { it thinks That's not what you said before! and generates the error message.

Declare or define your functions before calling them. C99 requires it. You get better error checking if you do that. You can define a static function before it is used without pre-declaring it.

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)
{
    …
}

…

If it was my code, all the functions except main() would be static (as above); if they were visible outside this source file, they'd be declared in a header and the header would be included here and wherever else the functions are used.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM