Skip to main content
Stack Overflow
  1. About
  2. For Teams

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

Required fields*

Array initialization

I have two types of array initialisation that will be used in string constructor

int main() {
 //char foo [] = { 'a', 'd' }; 
 char foo[] = "ad";
 std::string s = foo;
 cout<<s;
 int i;
 cin >> i;
}

Why in char foo [] = { 'a', 'd' }; case i have output:

ad╠╠╠╠╠╠R81ドル↑しかく

And when array is initialized like char foo [] = "ad"; I have normal output -ad- that was expected in first case.

What is difference in these two array initialization and why I have garbage in output in first one?

Answer*

Draft saved
Draft discarded
Cancel
1
  • "as compiler will generate a null terminate C string for you" Right, but explain why. Explain what difference in the code leads to this happening. Commented Oct 5, 2015 at 13:14

lang-cpp

AltStyle によって変換されたページ (->オリジナル) /