简体   繁体   English

指向字符数组数组的指针

[英]Pointer to array of character arrays

Okay, this one has me stumped. 好吧,这个让我难过。 I am trying to pass an array of character arrays into my class's constructor. 我试图将一个字符数组数组传递给我的类的构造函数。 The class has a private attribute which stores a pointer to the array of character arrays. 该类具有private属性,该属性存储指向字符数组数组的指针。 The class may then process the array via the pointer. 然后,类可以通过指针处理数组。

Below is some code that demonstrates the desired functionality. 下面是一些演示所需功能的代码。 But, it won't compile. 但是,它不会编译。 How do I fix this code so it works? 如何修复此代码以使其有效?

using namespace std;

const int MAX_LINES = 10, MAX_STRING = 80;

class Alphabetizer{
    public:
        Alphabetizer(char * inArray[][MAX_STRING]) : input(inArray){};

    private:
        char * input[MAX_LINES][MAX_STRING];
};

int main(){
    char charArray[MAX_LINES][MAX_STRING];
    Alphabetizer theAlaphBet(charArray);
    return 0;
}

If you're insisting on using C-compatible character pointers, I think you'll have the best luck using a char ** as the type for input . 如果你坚持使用C兼容的字符指针,我认为使用char **作为input类型你会有最好的运气。 This is more of the usual way to do this (in C at least), and it has the added benefit of not forcing you to define a maximum string size. 这是更常用的方法(至少在C语言中),它还有一个额外的好处,就是不要强制你定义最大字符串大小。

As others have pointed out, you can take advantage of std::string instead, which may be a better choice overall. 正如其他人所指出的那样,你可以利用std::string代替,这可能是一个更好的选择。

I'm guessing it's that you're not passing a pointer to char[][] , you're passing a char[][] . 我猜你没有传递指向char[][]的指针,你传递的是char[][]

Also, you should be using std::string instead of char arrays. 此外,您应该使用std::string而不是char数组。

std::string will be the most appropriate here! std::string将是最合适的! It handles strings and character arrays well enough! 它足够好地处理字符串和字符数组!

There are few errors in the code. 代码中的错误很少。 I suppose you are trying to refer to the charArray in the main function from inside the Alphabetizer object. 我想你试图从Alphabetizer对象里面引用main函数中的charArray。 If that is the case the declaration 如果是这样的声明

char * input[MAX_LINES][MAX_STRING];

is wrong because the above declaration makes input an array of MAX_LINE of ( array of MAX_STRING of (char*)). 是错误的,因为上面的声明使得输入一个MAX_LINE数组((char *)的MAX_STRING数组)。 In summary input is an array not a pointer to array of whatever. 总之,输入是一个数组,而不是指向任何数组的指针。 If you had intended it to be a pointer - which is what rest of your code hints to me - then you have to do the following, 如果您打算将它作为指针 - 这是您的其他代码提示给我的 - 那么您必须执行以下操作,

const int MAX_LINES = 10, MAX_STRING = 80;

class Alphabetizer{
    public:
        Alphabetizer(char ((*ar)[MAX_LINES])[MAX_STRING]) : m_ar(ar){};

    private:
        char ((*m_ar)[10])[80];
};

int main(){
    char charArray[MAX_LINES][MAX_STRING];
    char ((*ar)[MAX_LINES])[MAX_STRING] = &charArray;
    Alphabetizer theAlaphBet(&charArray);
    return 0;
}

Moreover doing, 做,

input(inArray)

is wrong, as it is equivalent to doing the following, 是错的,因为它等同于做以下事情,

char a[1] = {'a'};
char b[1] = {'p'};
a = b;

assigning an array to another does not copy one over another. 将数组分配给另一个数组不会将数据复制到另一个数组。 You have to do explicit memcpy. 你必须做显式的memcpy。 (This semantics is not meaningful in c or c++) (这种语义在c或c ++中没有意义)

It's difficult to tell without seeing the compile errors, but I think the problem might be this line: 没有看到编译错误很难说,但我认为问题可能就是这一行:

Alphabetizer theAlaphBet(charArray);

You are passing the array directly rather than it's address. 您是直接传递数组而不是它的地址。 It should read: 它应该是:

Alphabetizer theAlaphBet( &charArray );

However I think you may be overcomplicating things. 但是我认为你可能会过度复杂化。 You might be better off using a reference rather than a pointer: 使用引用而不是指针可能会更好:

const int MAX_LINES = 10, MAX_STRING = 80;

class Alphabetizer{
    public:
        Alphabetizer(char & inArray[][MAX_STRING]) : input(inArray){};

    private:
        char & input[MAX_LINES][MAX_STRING];
};

int main(){
    char charArray[MAX_LINES][MAX_STRING];
    Alphabetizer theAlaphBet(charArray);
    return 0;

} }

You might also want to look into using std::string instead as this may help to simplify your code. 您可能还想查看使用std :: string,因为这可能有助于简化代码。

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

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