1
int main() {
    
    string str[5] = "ABCD";
    std::cout << str[3] << std::endl;
    std::cout << str[0] << std::endl;
    return 0;
}

This code prints:

ABCD

ABCD

I didn't get it, how str[3] prints ABCD?

Compiler: GCC 6.3

Community
  • 1
  • 1
Debu Shinobi
  • 2,057
  • 18
  • 21

3 Answers3

7

The code is not valid C++ code and it shouldn't compile. And it doesn't with clang and gcc version above 7. It is most likely a bug in older version of gcc that got fixed in version 7.

std::string str[5]

What you have here is a C array of 5 elements of std::string. This is how you would initialize it:

std::string strings[5] = {"1st string", "2nd string", "3rd string", "4th string", "5th string"};

In this case strings[0] would be "1st string" and strings[3] would be "4th string".

However don't do this. Don't use C arrays in C++. Use std::vector or std::array if you need an array of strings: std::array<std::string> strings or std::vector<std::string> strings.

That being said, I suspect that you want just one string, aka:

std::string str = "ABCD";

In this case str[0] is 'A' and str[3] is 'D'

bolov
  • 72,283
  • 15
  • 145
  • 224
1

You can try this to get your code compile:

string str[5] = {"ABCD", "", "", "", ""};

str[3] in your code means 4the string of the array if you get it compiled.

But you most likely meant to have:

char str[5] = "ABCD";

string and char are not the same thing. If you want to access single char in a string you don’t need an array of strings. A single string is all you need:

string str = "ABCD";

std::cout << str[3] << std::endl;
Oblivion
  • 7,176
  • 2
  • 14
  • 33
  • 1
    This does not answer OP's question. – vitruvius Nov 15 '19 at 07:01
  • @BarisYakut it should now. – Oblivion Nov 15 '19 at 07:04
  • 1
    @OblivionreinstateOurMonica I sure can try that but I didn't understand how str[3] working? I mean where it is pointing to? – Debu Shinobi Nov 15 '19 at 07:05
  • @Sicario if you need 4th char of a string you need to try the last code snippet. – Oblivion Nov 15 '19 at 07:08
  • @Sicario in your code you have an array of string and str[3] points to the 4th string of the array. You need the 4th char of a string so don’t create an array of strings. – Oblivion Nov 15 '19 at 07:11
  • 1
    @OblivionreinstateOurMonica I tried that last snippet before and I got the result I wanted. What I wanted to know is what's going on in str[3]. – Debu Shinobi Nov 15 '19 at 07:12
  • 1
    @Sicario in my snippet the last str[3] points to the 4th char of the string str. string has [] operator and at function which give you access to the chars of the string. So string is kinda similar to vector if that makes easier to understand – Oblivion Nov 15 '19 at 07:15
-1

What's happening is:

the line string str[5] = "ABCD"; is simply copying/initializing all the indexes of variable str with the value ABCD like str[0] = "ABCD", str[1] = "ABCD", and so on.

So, when you run std::cout << str[3] << std::endl; and std::cout << str[0] << std::endl;, you're getting the value from the respective index of the string array str.

UpAndAdam
  • 4,515
  • 3
  • 28
  • 46
OMi Shah
  • 5,768
  • 3
  • 25
  • 34
  • 2
    This is what I was thinking. int main() { string str[5] = "ABCD"; std::cout << str[1] << std::endl; std::cout << str[2] << std::endl; std::cout << str[3] << std::endl; std::cout << str[4] << std::endl; std::cout << str[0] << std::endl; return 0; } prints ABCD 5 times. – Debu Shinobi Nov 15 '19 at 07:26
  • Can you please explain then how it's happening so? @bolov – OMi Shah Nov 15 '19 at 15:03
  • 2
    @OMiShah I did in my answer https://stackoverflow.com/a/58872004/2805305 . If there is something not clear from that feel free to ask me here or on my answer. – bolov Nov 15 '19 at 15:05
  • @UpAndAdam: The code is invalid and shouldn't compile, that's true. However, it *is* compiling, and in the OP's case, it *is* initializing and copying all the indexes with the value `"ABCD"`. Which means this post *is* answering the question "how str[3] prints ABCD?" – Mooing Duck Aug 22 '23 at 20:31
  • hence why i didnt downvote it :-) i'll reword my comment – UpAndAdam Aug 23 '23 at 13:53
  • Because it should not be compiling, and that it does leaves it up to compiler specific behavior. it's not going to behave this way for all compilers or versions of compilers and so folks dont want to encourage this understanding. – UpAndAdam Aug 23 '23 at 13:54