我收到“不兼容的指针类型"我不明白为什么 [英] I'm getting "incompatible pointer type" and I don't understand why

查看:85
本文介绍了我收到“不兼容的指针类型"我不明白为什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到两种错误:

编译器的投诉

pr.c:在"main"功能中:

pr.c: In function ‘main’:

pr.c:20:2:警告:从不兼容的地方传递"printMatrix"的参数1 指针类型[默认启用]

pr.c:20:2: warning: passing argument 1 of ‘printMatrix’ from incompatible pointer type [enabled by default]

pr.c:9:6:注意:预期为"const int(*)[80]",但参数的类型为"int(*)[80]"

pr.c:9:6: note: expected ‘const int (*)[80]’ but argument is of type ‘int (*)[80]’

pr.c:22:2:警告:从不兼容的指针类型[默认启用]传递"lights"的参数1

pr.c:22:2: warning: passing argument 1 of ‘lights’ from incompatible pointer type [enabled by default]

pr.c:10:6:注意:预期为"const int(*)[80]",但参数的类型为"int(*)[80]"

pr.c:10:6: note: expected ‘const int (*)[80]’ but argument is of type ‘int (*)[80]’

似乎编译器抱怨在接受带有const的函数中接收到非const,但是我被告知这是使用const ...

It seems the compiler complains about receiving a non-const in a function that takes a const, but I was told this was the correct way of using const...

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

#define MAXCOL  80
#define MAXROW  20
#define randNormalize() srand(time(0))

void fillMatrix(int m[][MAXCOL], size_t rows, size_t cols);
void printMatrix(const int m[][MAXCOL], size_t rows, size_t cols);
void lights(const int m[][MAXCOL], size_t rows, size_t cols);
int star(const int m[][MAXCOL], int row, int col);

int main()
{
    int m[MAXROW][MAXCOL];

    randNormalize();

    fillMatrix(m, 5, 5);
    printMatrix(m, 5, 5);

    lights(m, 5, 5);

    return 0;
}

void fillMatrix(int m[][MAXCOL], size_t rows, size_t cols)
{
    int i, j;

    for(i = 0; i < rows; i++)
        for(j = 0; j < cols; j++)
            m[i][j] = rand()%21;

}

void printMatrix(const int m[][MAXCOL], size_t rows, size_t cols)
{
    int i, j;

    for(i = 0; i < rows; i++)
    {
        printf("\n");

        for(j = 0; j < cols; j++)
            printf("%d ", m[i][j]);
    }

    printf("\n");
}


void lights(const int m[][MAXCOL], size_t rows, size_t cols)
{
    int i, j;

    for(i = 1; i < rows - 1; i++)
    {
        printf("\n");

        for(j = 1; j < cols - 1; j++)
        {
            if( star(m, i, j) )
                printf("*");
            else
                printf(" ");
        }
    }

    printf("\n");
}



int star(const int m[][MAXCOL], int row, int col)
{
    int i, j;
    int sum = 0;

    for(i = row - 1; i <= row + 1; i++)
        for(j = col - 1 ; j <= col + 1; j++ )
            sum += m[i][j];

    return (sum/9 > 10);
}

我正在寻找一种不使用指针的最佳解决方案,因为这是我们尚未涵盖它们(尽管我已经研究了它们)的一门课程的练习.

I'm looking for the best solution that doesn't use pointers, as this was from an exercise of a course in which we have not yet covered them (although I have studied them).

推荐答案

不幸的是,在C语言中,没有从int[X][Y]const int[X][Y]的隐式转换.也没有从int (*)[Y]const int (*)[Y]的隐式转换.

Unfortunately, in C there is no implicit conversion from int[X][Y] to const int[X][Y]. Nor is there implicit conversion from int (*)[Y] to const int (*)[Y].

这是语言上的缺陷;没有技术上的原因为什么不允许这种转换. (C ++确实允许这种转换).

This is a defect in the language; there's no technical reason why such a conversion should not be allowed. (C++ does allow this conversion).

您有两种选择,都没有吸引力:

You have two options, both unappealing:

  1. 让函数接受int而不是const int
  2. 在调用const int函数时编写强制类型转换,例如printMatrix((const int (*)[MAXCOL])m, 5, 5);
  1. Have the functions accept int instead of const int
  2. Write a cast when calling the const int functions, e.g. printMatrix((const int (*)[MAXCOL])m, 5, 5);

通常会使用选项1,对于多维数组,我们只需要在没有const正确性的情况下即可.

Normally option 1 would be used and we just have to do without const-correctness for multi-dimensional arrays.

这篇关于我收到“不兼容的指针类型"我不明白为什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆