Hi there,
I've just started to pick up C++. I've been trying to figure out what's wrong with the following code for a few hours. It probably is a simple and trivial error rooted in something I have understood wrong about the concepts of pointers and arrays in VC++ to which I'm new. So apologies and thanks in advance.
I'm trying to define an array of pointers (to hold a series of text) in the header file:
class ...
{
..
private:
char *array[];
}
and in the c++ file:
Constructor... or some other method
{
array = new char*[]; //Error line
}
I've tried a variety of the Error line and I get different error messages (all error C2440 which has something to do with incompatible types). If I change the line to:
char* array = new char[];
then it works fine. But this is a local and different declaration and am not even sure that is an array of chars.
So could you please tell me what is wrong here? I'd appreciate a little explanation as I'm sure I have misunderstood some sort of concept here.
Thank you very much for your time.
BTW, I know I can use some other types of ready-made arrays which make the job easier. I'm not writing anything special at the moment, I'm just trying to understand VC++ here and get the concepts.
And another question, when I'm trying to:
delete [] array;
I get "warning C4154: declaration of a array expression; conversion to pointer supplied." Why am I getting this here?
Thanks again. Looking forward to some enlightenment here
Thank you for your reply. But as I said, I am not using this code to write any special program. I just want to learn the concepts of pointers and arrays in C++ correctly. And I think it is quite important to understand why this piece of code is not working.
EDIT: I want you to notice that using the pointer approach allows you to set the size in run-time but doesn't allow you to resize the array. Using a string vector doesn't have this limitation.
Thank you so much for your reply. Your code helped me a lot to understand the concepts of pointers and arrays much better :-)
I've changed my experimental code and it works now. Tho I still have a question. I declare my pointer as follows (in the header file):
char *array[];
(tho during compilation I get C4200 warning: nonstandard extension used: zero-sized array in struct/union. Is this any important?)
and later in the constructor of the cpp file:
array[0] = new char(10);
array[0] = "Undefined\0";
And I later on dynamically manage the contents of the array. However, the question I have is this:
What is the size of this array? I used to use sizeof(array)/sizeof(array[0]) to get the number of elements in this array. This doesn't work anymore (I believe due to the fact that the size of array is not specified). I also can set the value of any cells... I mean I can write
What you want is a double pointer. With your syntax you declare an array of pointers. When you do that, you must also specify the size of the array, which you don't.
This:
fds' compiler said:
C4200 warning: nonstandard extension used: zero-sized array in struct/union.
also gives you a hint. It tells you that the array you just created has a size of zero! This is non-standard code and not all compilers will allow you to do it.