Array of char pointers

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

F
I'd say that it's better not to mess with pointers. Instead of an array of char pointers use a vector of strings. It is both safer and easier to use.

Info on vector -> http://cplusplus.com/reference/stl/vector/
Info on string -> http://cplusplus.com/reference/string/string/
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.
http://cplusplus.com/forum/beginner/23390/

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.
Last edited on

The following line you have to specify the number of pointer's to char array's you want.

array = new char*[NEED_CONSTANT_HERE]; // has to be specified at compile time

If you want to specify this size at runtime you need to define it as follows

1
2
char** array;
array = new char * [VARIABLE_OR_CONSTANT_HERE]; // could be entered during runtime 


Following is some example code I wrote:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
	
  // pointer to array of 4 char's, max string it can hold is 3 + NULL terminator
  char *pPtrToChars = new char [4];
  strcpy( pPtrToChars, "ABC" );

  // array of pointers to array's of char's
  // size is fixed at 2 during compile time
  char *ArrayOfCharPtrs[2];
  ArrayOfCharPtrs[0] = new char [4];
  ArrayOfCharPtrs[1] = new char [4];
  strcpy( ArrayOfCharPtrs[0], "DEF" );
  strcpy( ArrayOfCharPtrs[1], "GHI" );

  // pointer to array of pointers to array of char's
  // size is dynamic, set at runtime
  int numArrays = 2;
  char **pPtrToArrayOfCharPtrs = new char * [numArrays];
  pPtrToArrayOfCharPtrs[0] = new char [4];
  pPtrToArrayOfCharPtrs[1] = new char [4];
  strcpy( pPtrToArrayOfCharPtrs[0], "JKL" );
  strcpy( pPtrToArrayOfCharPtrs[1], "MNO" );

  cout << pPtrToChars              << endl;
  cout << ArrayOfCharPtrs[0]       << endl;
  cout << ArrayOfCharPtrs[1]       << endl;
  cout << pPtrToArrayOfCharPtrs[0] << endl;
  cout << pPtrToArrayOfCharPtrs[1] << endl;


Program Output:

ABC
DEF
GHI
JKL
MNO
Last edited on
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

array[50] = new char(20);

Why? What is the last index number of this array?

Thanks a lot.
F
Don't do it like this -> char *array[];

Do it like this -> char ** array;

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.

Here is more info on zero sized arrays -> http://learningcppisfun.blogspot.com/2008/02/zero-sized-arrays.html
(even though this is not what you want here)

Ok... these here are not correct at all...

1
2
array[0] = new char(10);
array[0] = "Undefined\0";

The above should be:

1
2
array[0] = new char[10];
strcpy(array[0],"Undefined\0");

fds wrote:
What is the size of this array?

Like I said before, zero.

fds wrote:
Why? What is the last index number of this array?

Modifying array[i] (for every i) overwrites important data of the program, as your array was declared to have zero size.

Did you check the link I gave you in my second reply? Another guy had exactly the same problem and I showed him how to do it.



Last edited on
Topic archived. No new replies allowed.