very quick array question

I am sure you get the gist of what I am trying to do; but if not I am trying to have an array of char potentially of different length, these are not user entered input or anything but declared at compile (hence why I am using arrays).

1
2
3
char[2][]charList;
charList[0] = new CharList{ '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
harList[1] = new CharList{ '+', '/', '-', '*', '=' };


this is not even attempt to compile; how should I write this.

thanks
Last edited on
You use the new operator. Dynamically allocate the char arrays.
Waitaminnit - you're already doing that, where's your class implementation?
If you must use char arrays:
 
char charList[2][] = { "0123456789", "+/-*=" };
You'd probably be better off with an array of std::string anyway.
thanks for the input just going to try that now.

this is not a string in anyway, i am looping through the array to for expression evaluation (from an ASCII input) so I see no reason to include an entire library for this situation.

thank you
Why not? You won't be able to tell the difference in overhead and string is far more efficient and easier to use than char[]. And it still supports subscripting across characters.
Sorry i just tried that and it would no compile
char charList[2][] = { "0123456789", "+/-*=" };

VS reported
array bounds overflow and missing subscript

thanks again
EDIT: Never mind, I see what you tried.
Anyway, that should work. But I'm no cstring expert, as I said I prefer std::string. With that the code should be relatively simple, although I don't remember how to do construction with an array initializer list. Only if you must use char[] should you really use it for this, you should be fine without.
Last edited on
I think the problem is with the empty subscript. You have charList[2][].
Make it charList[2][10]. This will leave unknown values in your second row after your entry =.
Technically it should be able to determine the second subscript from the number of elements in the initializer list.
@OP: did you read the article Bazzy directed you to? It's the MD array article; it may have some tips or suggestions for this problem.
EDIT: Whoa, there are two MD articles on this forum! I never knew... OP, Disch's article on MD array pitfalls may also come in handy then, since that one explains how to engender them:
http://www.cplusplus.com/forum/articles/17108/
Last edited on
If I get this right the real problem here is that C++ does like declaring an arrays of arrays with different depths?

so would it be better to create an array ([2]) of char*

if so how the hell would i go about that. as I said this is not to form a string but an actual list of chars: so if I can help from including string and vectors that would be great
the solution is
 
char* charList[] = {"0123456789","+/-*="};


with this declaration you are able to loop through the array, e.g.,
1
2
3
charList[0][0] = '0';
charList[0][1] = '1';
charList[0][2] = '2';


and

1
2
3
charList[1][0] = '+';
charList[1][1] = '/';
charList[1][2] = '-';

best! that worked

I might post my listing for the method up later so you can glance over it and see if it done correctly.
Topic archived. No new replies allowed.