Array of chars

P

Peter

Hi,

I want to use an array str, while each array element str
is an array of chars of fixed length, say 40;

The best way to handle this in C# is StringBuilder I guess,
but as legacy C++ code must communicate with my C# app, I
prefer only low-level primitive types, so chars here.

Of course, char [] str = new char[40]; will create an array
of 40 chars for me, but that's not what I want.

I need something like :

for (int i=0;i<50;i++)
{
char [] str = new char[40];
}
I guess, but how to dimension the array str itself. By
using char [] [] .... ????

Confused with the double dimensions, one for the array
lenght of str , and one for the number of chars in each
array element ...

Some help would be nice.

Thanks,
Peter
 
S

Stephen Brooker

Peter said:
Hi,

I want to use an array str, while each array element str
is an array of chars of fixed length, say 40;

The best way to handle this in C# is StringBuilder I guess,
but as legacy C++ code must communicate with my C# app, I
prefer only low-level primitive types, so chars here.

Of course, char [] str = new char[40]; will create an array
of 40 chars for me, but that's not what I want.

I need something like :

for (int i=0;i<50;i++)
{
char [] str = new char[40];
}
I guess, but how to dimension the array str itself. By
using char [] [] .... ????

Confused with the double dimensions, one for the array
lenght of str , and one for the number of chars in each
array element ...

Some help would be nice.

Thanks,
Peter


Do a search for multidimensional arrays in the MSDN library.

You want something like:
char[,] myArray = new char[10,40];
 
?

=?ISO-8859-1?Q?=22Anders_Nor=E5s_=5BMCAD=5D=22?=

Peter said:
Hi,

I want to use an array str, while each array element str
is an array of chars of fixed length, say 40;
(abridged)
I need something like :

for (int i=0;i<50;i++)
{
char [] str = new char[40];
}

You've got two options. If every char array is the same length, eg. 40.
You can use a multidimensional array:
char[,] str=new char[40,40];

If the arrays aren't the same length, you must use a jagged array which
is an array-of-arrays.
char[][] str=new char[40][];
for (int i=0; i<40;i++) {
str=new string[i*2];
}

Anders Norås
http://dotnetjunkies.com/weblog/anoras/
 
J

Jon Skeet [C# MVP]

Peter said:
I want to use an array str, while each array element str
is an array of chars of fixed length, say 40;

The best way to handle this in C# is StringBuilder I guess,
but as legacy C++ code must communicate with my C# app, I
prefer only low-level primitive types, so chars here.

Of course, char [] str = new char[40]; will create an array
of 40 chars for me, but that's not what I want.

I need something like :

for (int i=0;i<50;i++)
{
char [] str = new char[40];
}
I guess, but how to dimension the array str itself. By
using char [] [] .... ????

Confused with the double dimensions, one for the array
lenght of str , and one for the number of chars in each
array element ...


char[][] str = new char[50][];

for (int i=0; i < 50; i++)
{
str = new char[40];
}
 
C

Cor Ligthert

Jon,

I was waiting for your answer because I thought there should be something
else, luckily for my self-coinfidence you did not show that as well. (And
still I am not sure)

:)

Cor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top