【发布时间】:2018-03-02 12:03:49
【问题描述】:
试图弄清楚为什么我的 char 数组在类或结构中不接受所有字符,因为它通常在不在类或结构中时会这样做。
#include <iostream>
using namespace std;
const int SIZE = 10;
struct A{
char address[SIZE];
}
int main(){
char address_from_main[SIZE];
A a;
address_from_main[2] = 9;
cout<<"address from main: "<<address_from_main[2]<<endl;
a.address[2] = 9;
a.address[3] = 'a';
cout<<"show 2: "<<a.address[2]<<" , but didnt show"<<endl;
cout<<"show 3: "<<a.address[3]<<" , this one did"<<endl;
output = address from main: 9\nshow 2: ,但没有显示 \nshow3: ,这个有
这怎么可能? 有人知道如何解决这个问题吗?
非常感谢。
【问题讨论】:
-
char和int显示不同...9 != '9'。 -
Cannot reproduce 第一个例子。它不输出
9。 -
anything.. 但它是一个比索引大的常数。说 100。
-
您是否可能在某些时候使用
'9'而不是9而只是忘记了' '? -
@Jarod42 UB??也许定义了实现。
标签: c++ arrays class struct char