Board index » cppbuilder » Array of arrays

Array of arrays


2003-09-19 02:59:46 AM
cppbuilder68
I need to generate a multidimensional array on the heap, and am having
trouble. I know this is must be rather elementary, but then, so am I.
If I declare; char roger[10][20]; I get a multidimensional array on the
stack -- no problem.
If I declare char *roger = new char[10]; I get a single-dimension array on
the heap, pointed to by *roger -- no problem.
If I declare char *roger = new char[10][20]; I get a compiler error.
I can get around the problem with a loop or two, but I'd like to know if
there is a simplier way -- there must be.
Pat
 
 

Re:Array of arrays

On Thu, 18 Sep 2003 11:59:46 -0700, "Pat McClure" < XXXX@XXXXX.COM >
wrote:
Quote
If I declare char *roger = new char[10][20]; I get a compiler error.
What is the error? Don't expect others to guess.
Also always state which compiler and version you're using.
Try this:
typedef char Array20ch[20];
Array20ch *roger = new Array20ch[10];
Example:
#include <iostream.h>
#include <stdio.h>
typedef char Array20ch[20];
int main()
{
// char *roger = new char[10][20];
Array20ch *roger = new Array20ch[10];
int i;
for(i=0; i<10; ++i)
{
sprintf(roger[i], "String number %d", i);
}
for(i=0; i<10; ++i)
{
cout << roger[i] << endl;
}
delete[] roger;
return 0;
}
P.S. - This question has nothing to do with the BC++ IDE. Please
post language questions in a more appropriate group, such as
borland.public.cpp.borlandcpp
borland.public.cppbuilder.language.cpp
borland.public.cppbuilder.students
 

Re:Array of arrays

"Pat McClure" < XXXX@XXXXX.COM >wrote in message
Quote
I need to generate a multidimensional array on the heap, and am having
trouble. I know this is must be rather elementary, but then, so am I.

If I declare; char roger[10][20]; I get a multidimensional array on the
stack -- no problem.

If I declare char *roger = new char[10]; I get a single-dimension array
on
the heap, pointed to by *roger -- no problem.

If I declare char *roger = new char[10][20]; I get a compiler error.

I can get around the problem with a loop or two, but I'd like to know if
there is a simplier way -- there must be.

Pat

try
char* *roger = new char[10][20];
ie roger is a pointer to a pointer now.
cheers
Warwick
---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (www.grisoft.com).
Version: 6.0.520 / Virus Database: 318 - Release Date: 9/18/2003
 

{smallsort}