PC Review


Reply
Thread Tools Rate Thread

Array of chars

 
 
Peter
Guest
Posts: n/a
 
      7th Jan 2005
Hi,

I want to use an array str, while each array element str[i]
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[i] = 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

 
Reply With Quote
 
 
 
 
Stephen Brooker
Guest
Posts: n/a
 
      7th Jan 2005
Peter wrote:
> Hi,
>
> I want to use an array str, while each array element str[i]
> 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[i] = 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];
 
Reply With Quote
 
=?ISO-8859-1?Q?=22Anders_Nor=E5s_=5BMCAD=5D=22?=
Guest
Posts: n/a
 
      7th Jan 2005
Peter wrote:
> Hi,
>
> I want to use an array str, while each array element str[i]
> is an array of chars of fixed length, say 40;
> (abridged)
> I need something like :
>
> for (int i=0;i<50;i++)
> {
> char [] str[i] = 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[i]=new string[i*2];
}

Anders Norås
http://dotnetjunkies.com/weblog/anoras/
 
Reply With Quote
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      7th Jan 2005
Peter <(E-Mail Removed)> wrote:
> I want to use an array str, while each array element str[i]
> 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[i] = 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[i] = new char[40];
}

--
Jon Skeet - <(E-Mail Removed)>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
 
Reply With Quote
 
Cor Ligthert
Guest
Posts: n/a
 
      7th Jan 2005
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


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Concatenating Chars in an Array eBob.com Microsoft VB .NET 17 2nd Sep 2008 05:14 AM
Array formulas + more than 255 chars gmac Microsoft Excel Programming 4 10th Apr 2007 07:57 PM
compare/check character against an array of chars - best practice? =?Utf-8?B?UmljaA==?= Microsoft VB .NET 3 14th Nov 2006 12:28 AM
converting an array of bytes to an array of chars Claire Microsoft C# .NET 1 8th Jun 2004 04:58 PM
Cannot initialize array of chars (System.TypeLoadException) DC Microsoft C# .NET 1 24th Jul 2003 08:32 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 04:31 PM.