如何将char数组声明为函数参数?或者告诉我此代码中还有其他错误? [英] How to declare char array as function parameter ?or tell me there is some other thing wrong in this code?

查看:113
本文介绍了如何将char数组声明为函数参数?或者告诉我此代码中还有其他错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我应该怎么做才能解决这个问题?

What should I do to resolve this ?

我一无所知,所以我什么也没尝试

i don't know any other way so i didn't try anything

bool isPresent(int p_rows,int p_cols,char p_search[p_cols'1']'2','3'char p_words[][p_cols])

单逗号计数表明我在那一点上收到的错误. 我还要提到的一件事是,我正在该程序中进行递归操作,还有其他函数的参数与此相同,但仅此函数定义会导致错误

The counting in single commas indicate which error I m receiving at which point. One more thing which I would like to mention is that I m doing recursion in this program and there are other functions also whose parameters are same as of this but only this function definition is causing errors

1)use of parameter outside function body before ']' token
2)expected ')' before ',' token`enter code here`
3)[Error] expected unqualified-id before 'char'

推荐答案

可变长度数组不是标准的C ++功能.但是在此声明中

Variable length arrays are not a standard C++ feature. However in this declaration

bool isPresent(int p_rows,int p_cols,char p_search[p_cols], char p_words[][p_cols]);
                                                   ^^^^^^                  ^^^^^^

您正在尝试使用变量数组声明.

you are trying to use variable arrays declarations.

最后一个参数声明

char p_words[][p_cols]

变量p_cols必须是编译时常量.

the variable p_cols must be a compile-time constant.

声明可以采用以下方式.我想在程序中声明了作为最后一个参数传递给函数的数组,例如具有固定常量尺寸的数组,并且变量p_rowsp_cols指定了该数组的子数组.

The declaration can look the following way. I suppose that the array passed to the function as the last argument is declared in a program like an array with fixed constant dimensions and the variables p_rows and p_cols specify a sub-array of the array.

bool isPresent(int p_rows,int p_cols,char p_search[], char p_words[][N]);

其中N是原始数组的第二个常量维.

where N is the second constant dimension of the original array.

例如

const size_t M = 100;
const size_t N = 100;

bool isPresent(int p_rows,int p_cols,char p_search[], char p_words[][N]);

int main()
{
    char p_words[M][N];
    char p_search[N];

    // ...    

    int p_rows = 10; 
    int p_cols = N;

    isPresent( p_rows, p_cols, p_search, p_words );

    //...
}

否则,您应使用例如std::vector<std::vector<char>>std::vector<std::string>std::vector<char>的数组或std::string的数组,而不是原始的数组定义.

Otherwise you should use for example std::vector<std::vector<char>>, or std::vector<std::string>, or an array of std::vector<char>, or an array of std::string instead of your original array definition.

这篇关于如何将char数组声明为函数参数?或者告诉我此代码中还有其他错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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