1

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. 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;
}
2
  • Shouldn't that be char * charArray[MAX_LINES]...? Commented Nov 6, 2012 at 4:26
  • or char charArray[MAX_LINES][MAX_STRING] Commented Nov 6, 2012 at 4:30

5 Answers 5

3

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. 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.

As others have pointed out, you can take advantage of std::string instead, which may be a better choice overall.

Sign up to request clarification or add additional context in comments.

Comments

0

I'm guessing it's that you're not passing a pointer to char[][], you're passing a char[][].

Also, you should be using std::string instead of char arrays.

Comments

0

std::string will be the most appropriate here! It handles strings and character arrays well enough!

Comments

0

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. 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*)). 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. (This semantics is not meaningful in c or c++)

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.