N00b101
N00b101

Reputation: 165

Visual C++ issue with dynamically created arrays

I have a 2d array of strings in visual C++ like this:

static array<System::String ^, 2> ^messages = {
{"a", "b"},
{"c", "d", "e"},
{"f"},
};

I want to print the data out like this:

for(int i = 0; i < 3; i++)
     for(int j = 0; j < messages[i]->Length; j++)     //ERROR
                    //print messages[i, j]

I can't get the length of a dimention of a dynamiclly created array. How can I do this?

Upvotes: 0

Views: 133

Answers (2)

Hans Passant
Hans Passant

Reputation: 942000

Your intentions are not entirely clear, but if you want to make that kind of for(;;) loop work then you must create a jagged array. An array of arrays, each element is an array than can have its own size.

ref class Test {
    static array<array<String^>^>^ messages = {
        gcnew array<String^> {"a", "b"},
        gcnew array<String^> {"c", "d", "e" },
        gcnew array<String^> {"f" }
    };
public:
    static void Run() {
        for (int i = 0; i < messages->Length; i++) {
            for (int j = 0; j < messages[i]->Length; j++)
                Console::Write("{0} ", messages[i][j]);
            Console::WriteLine();
        }
    }
};

Output:

a b
c d e
f

Upvotes: 3

David Yaw
David Yaw

Reputation: 27864

array<System::String^, 2> declares a two dimensional array. Arrays created in this way have a fixed height & width.

array<String^, 2>^ messages = 
{
    {"a", "b"},
    {"c", "d", "e"},
    {"f"},
};

Debug::WriteLine("The total number of array elements is {0}", messages->Length);
Debug::WriteLine("Dimension 0 has a length of {0}", messages->GetLength(0));
Debug::WriteLine("Dimension 1 has a length of {0}", messages->GetLength(1));

Debug::WriteLine("{");
for (int i = 0; i < messages->GetLength(0); i++)
{
    Debug::Write("    { ");
    for (int j = 0; j < messages->GetLength(1); j++)
    {
        Debug::Write("\"" + messages[i,j] + "\", ");
    }
    Debug::WriteLine("}");
}
Debug::WriteLine("}");

Output:

The total number of array elements is 9
Dimension 0 has a length of 3
Dimension 1 has a length of 3
{
    { "a", "b", "", }
    { "c", "d", "e", }
    { "f", "", "", }
}

However, based on how you're initializing the array, and how you're looping, it looks like you're interested in a jagged array, not a regular two dimensional (square/rectangular) array. This should be declared as an array of arrays: Each inner array is initialized separately, and therefore each has their own length.

array<array<String^>^>^ messages = 
{
    {"a", "b"},
    {"c", "d", "e"},
    {"f"},
};

// The only thing we can tell easily is the number of rows. The total 
// number of elements and the column count would require iteration.
Debug::WriteLine("There are {0} rows in the jagged array", messages->Length);

Debug::WriteLine("{");
for (int i = 0; i < messages->Length; i++)
{
    Debug::Write("    { ");
    for (int j = 0; j < messages[i]->Length; j++)
    {
        Debug::Write("\"" + messages[i][j] + "\", ");
    }
    Debug::WriteLine("}");
}
Debug::WriteLine("}");

Output:

There are 3 rows in the jagged array
{
    { "a", "b", }
    { "c", "d", "e", }
    { "f", }
}

Upvotes: 2

Related Questions