0

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?

4
  • 2
    This is managed C++. Please add the appropriate tag. Commented May 16, 2014 at 17:01
  • @RSahu This is not Managed C++, but C++/CLI. Different syntax! Commented May 16, 2014 at 17:16
  • Looks like there might be some hints in the documentation's examples. Commented May 16, 2014 at 17:19
  • Also don't know if you can do a "jagged" array like this without having an array of arrays - Arrays in C++/CLI Commented May 16, 2014 at 17:30

2 Answers 2

3

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
Sign up to request clarification or add additional context in comments.

Comments

2

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", }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.