简体   繁体   English

int(*)[] 类型的参数与“int**”类型的参数不兼容

[英]Argument of type int(*)[] is incompatible with parameter of type “int**”

I was creating a function that would give me random values of a matrix.我正在创建一个 function ,它会给我一个矩阵的随机值。 I know how to make it in the main function but I want a separate one.我知道如何在main function 中制作它,但我想要一个单独的。 I also tried with void and the same thing happened, and I keep getting the same error.我也尝试过void并且发生了同样的事情,并且我不断收到同样的错误。 I am beginner and I understand that the issue is something to do with pointers but I still don't know what should I do to make it work and fix it.我是初学者,我知道这个问题与指针有关,但我仍然不知道我应该怎么做才能让它工作并修复它。 I already Googled it but I can't make this work.我已经用谷歌搜索了它,但我无法完成这项工作。

#include<stdlib.h>
#include<iostream> 
#include<time.h>

int row, coll;

int random(int *mat[])
{
    srand(time(0));
    for(int i=0; i<row; i++)
    {
        for(int j=0; j<coll; j++)
        {
            mat[i][j] = rand()%10;
        }
    }

}

main()
{
    std::cin >> row >> coll;
    int mat[row][coll];
    random(mat);
} 

You are trying to mix features of two different languages in one program.您正试图在一个程序中混合两种不同语言的功能。

The presented program is not a valid C++ program nor a valid C program.提供的程序不是有效的 C++ 程序,也不是有效的 C 程序。

For example variable length arrays as in your program例如在您的程序中的可变长度 arrays

std::cin >> row >> coll;
int mat[row][coll];

is not a standard C++ feature.不是标准的 C++ 功能。

The function main shall have the return type int . function main 应具有返回类型int

Your function random你的 function random

int random(int *mat[])

has the return type int but returns nothing.具有返回类型int但不返回任何内容。

The argument has the type (if to assume that variable length arrays are supported)参数具有类型(如果假设支持可变长度 arrays)

int ( * )[coll]

but the function parameter type is但 function 参数类型是

int **.

If you are going to write a C++ program then instead of a variable length array use the standard container std::vector<std::vector<int>> .如果您要编写 C++ 程序,则使用标准容器std::vector<std::vector<int>>代替可变长度数组。

For example例如

std::cin >> row >> coll;

std::vector<std::vector<int>> mat( row, std::vector<int>( coll ) );

Otherwise write a C program that can look like否则写一个 C 程序,看起来像

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

void random( size_t row, size_t col, int mat[row][col], int limit )
{
    srand( ( unsigned int )time( NULL ) );
    
    for ( size_t i = 0; i < row; i++ )
    {
        for ( size_t j = 0; j < col; j++ )
        {
            mat[i][j] = rand() % limit;
        }
    }
}

int main( void ) 
{
    size_t row, col;
    
    scanf( "%zu %zu", &row, &col );
    
    int mat[row][col];
    
    random( row, col, mat, 10 );
    
    return 0;
}

I wrote a simple struct of matrix for you to carry the data around functions.我为你写了一个简单的矩阵结构来携带函数周围的数据。 Also, a output overload to make it easier to demo.此外,output 过载,使其更容易演示。 Please look over it, and don't hesitate to ask, if you need more elaborations.请查看它,如果您需要更多详细信息,请不要犹豫。

#include<stdlib.h>
#include<iostream>
#include<time.h>
#include <iomanip>
struct Matrix
{
    int *arr;
    const int row, col;
    Matrix() = delete;
    Matrix(const int i, const int j): row(i), col(j)
     {
       arr = new int [i*j];
     }
   ~Matrix() { delete [] arr; }
   int operator()(const int i, const int j) const {return arr[i*col+j];}
   int&operator()(const int i, const int j) {return arr[i*col+j];}
};
std::ostream& operator<<(std::ostream&osm, const Matrix&M)
{
    for (int i=0; i<M.row; i++) {
        for (int j=0; j< M.col; j++)  osm << std::setw(8) << M(i,j);
        osm << std::endl;
    }
    return osm;
}
void random(Matrix& amtx)
{
   srand(time(0));
   for(int i=0; i<amtx.row; i++)
    {
       for(int j=0; j<amtx.col; j++)
        {
           amtx(i,j) = rand()%10;
        }
    }
}
int main()
{
    int r, c;
    std::cout << "Input row and column : ";
    std::cin >> r >> c;
    Matrix mtx(r, c);
    random(mtx);
    std::cout << mtx;
   return 0;
}

and the test run:和测试运行:

Input row and column :  5  5
      6       3       1       9       3
      6       5       1       4       1
      1       2       7       5       5
      9       3       2       8       1
      7       4       8       3       5

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 “int”类型的参数与“int*”类型的参数不兼容 - argument of type “int” incompatible with parameter of type “int*” 类型为“ int”的参数与类型为“ int *”的参数不兼容 - Argument of type “int” is incompatible with parameter of type “int *” 类型“ int”的参数与参数类型“ int **”不兼容 - argument of type “int” is incompatible with parameter type “int **” 类型“int”的参数与类型“int”的参数不兼容 - argument of type “int” incompatible with parameter of type “int” “ int”类型的参数与“ char”类型的参数不兼容 - Argument of type 'int' is incompatible with parameter of type 'char' 错误:参数类型int与参数类型不兼容 - ERROR: argument type int incompatible for parameter type “类型为 &#39;int(*)()&#39; 的参数与类型为 int 的参数不兼容”错误? - "argument of type ”int(*)()“ is incompatible with parameter of type int" error? “int*”类型的参数与“int**”类型的参数不兼容 C++ 中的错误 - argument of type “int*” is incompatible with parameter of type “int**” error in C++ “unsigned int *”类型的参数与“size_t *”类型的参数不兼容 - argument of type “unsigned int *” is incompatible with parameter of type “size_t *” “void(*)(int wall)”类型的 C++ 参数与“int”类型的参数不兼容 - C++ argument of type “void(*)(int wall) is incompatible with parameter of type ”int"
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM