Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding multidimensional string array in C++

I am new to c++. I can not understand why the following code prints the "r" string. I think the it should be an array of 2X3X4 elements, so by pointing to the arr[0][0][0] i would expect the first char in the first string of the first arr=a, but this prints abcd. Can anyone explain it?

#include <iostream>

using namespace std;

int main()
{
  string arr [2] [3] [4]={
    {"abcd","efgh","ijkl"},
    {"mnop","qrst","xywz"}
  };

  cout<<arr [1] [0] [1] [1]<<endl;

  return 0;
}

Edit:

What makes me confused is the behavior in python. The following python code prints a:

arr=[["abcd","efgh","ijkl"],["mnop","qrst","xwyz"]]

print arr[0][0][0]

It addresses to the first letter of the first string in the first list.

I would think that the equivalent of this in c++ would be:

#include <iostream>

using namespace std;

int main()
{
  string arr [2] [3] [4]={
    {"abcd","efgh","ijkl"},
    {"mnop","qrst","xywz"}
  };

  cout<<arr[0][0][0]<<endl;

  return 0;
}

by pointing to the first letter in the first string of the first array. But this prints the first string abcd. My question is why should i put another [0] in order to get to the a?


2 Answers

Your initializer populates the array as follows:

arr[0][0][0] = "abcd";
arr[0][0][1] = "efgh";
arr[0][0][2] = "ijkl";
arr[1][0][0] = "mnop";
arr[1][0][1] = "qrst";
arr[1][0][2] = "xywz";

All other elements are default-initialized to empty string.

Thus, arr[1][0][1] is the string containing "qrst", and arr[1][0][1][1] is the second character of that string, namely 'r'.

like image 192
Igor Tandetnik Avatar answered Aug 04 '26 07:08

Igor Tandetnik


You've confused the standard library string object with the concept of a c-string/string literal, and you've helped yourself with this by avoiding the use of the std:: prefix. If we add this, it starts to make more sense:

std::string arr [2] [3] [4]={
  {"abcd","efgh","ijkl"},
  {"mnop","qrst","xywz"}
};

What you are declaring here is an array of 2 x 3 x 4 instances of std::string. But what you wrote looks like you thought you were declaring character arrays:

char arr [2] [3] [4] = {
  {"abcd","efgh","ijkl"},
  {"mnop","qrst","xywz"}
};

would almost have the effect you were trying to achieve -- in this case arr[0][0][0] does point to a rather than the string.

Unfortunately the problem here is that you've specified a final dimension of 4 and then supplied 5-character c-strings to the initializer. Remember:

"abcd"

is equivalent to

{ 'a', 'b', 'c', 'd', 0 }

because c-strings are nul-terminated. So you would need to write

char arr [2] [3] [5] = {
  {"abcd","efgh","ijkl"},
  {"mnop","qrst","xywz"}
};

or, if what you actually want is specifically arrays of characters, not nul-terminated c-strings:

charr arr[2][3][4] = {
    { { 'a', 'b', 'c', 'd' }, { 'e', 'f', 'g', 'h' }, ...

std::string is a discrete object, not an alias for a c-string.

#include <iostream>
#include <string>

int main() {
    std::string arr[2][3] = {
        { "abcd", "efgh", "ijkl" },
        { "mnop", "qrst", "wxyz" },  // who needs 'u' or 'v'?
    };

    std::cout << "arr[0][0] = " << arr[0][0] << "\n";
    std::cout << "arr[0][0][0] = " << arr[0][0][0] << "\n";

}

http://ideone.com/JQrDxr

like image 41
kfsone Avatar answered Aug 04 '26 06:08

kfsone



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!