![](/img/trans.png)
[英]Array gives correct output for this mergesort function but vector is giving incorrect output. What is going wrong?
[英]giving char value as array index gives wrong output
我給char c,value(c ='1')作為要打印的數組索引,它給了我錯誤的輸出
#include<iostream>
using namespace std;
int main()
{
int a[5];
cin>>a[1]; //5
char c ='1';
cout<<c<<" "<<a[c];
}
我希望輸出5,但實際輸出是一些隨機數
您的輸出代碼等效於:
cout << c << " " << a[int(c)];
換一種說法:
cout << '1' << " " << a[49];
那只是一個未定義的行為。 享受鼻惡魔。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.