C++ 为什么打印字符串数组会输出十六进制?
为什么以下程序将“0x2ffee4”打印到控制台C++ 为什么打印字符串数组会输出十六进制?,c++,string,c++11,C++,String,C++11,为什么以下程序将“0x2ffee4”打印到控制台 #include <string> #include <iostream> using namespace std; int main() { string city1[] = "toronto"; cout << city1; return 0; } #包括 #包括 使用名称空间std; int main(){ 字符串city1[]=“多伦多”; cout第一行不应该编译,但是有
#include <string>
#include <iostream>
using namespace std;
int main() {
string city1[] = "toronto";
cout << city1;
return 0;
}
#包括
#包括
使用名称空间std;
int main(){
字符串city1[]=“多伦多”;
cout第一行不应该编译,但是有一个
std::string city1[] = {"toronto", "toronto", "toronto", "toronto",
"toronto", "toronto", "toronto", "toronto"};
(8,因为“toronto”
是8个字符,包括终止的null。是的,这意味着如果使用,它将创建一个包含46个字符串的数组,每个字符串存储“chargoggagoggmanchaugogchaugchaubungangamaugg”
)
不用说,您不应该依赖于编译器bug
在GCC的错误行为下,city1
将是一个std::string
s数组;没有操作符T.C.给出的答案是正确的,我还想指出,如果您希望使用cout将“toronto”打印到控制台,那么您需要这样做:
include <string>
include <iostream>
int main() {
using namespace std;
// string city1[] = "toronto"; // Compiler Error - Do Next Line Instead
string city1[] = { "toronto" };
cout << city1[0];
return 0;
}
}
如果您没有使用带索引值的括号运算符发送到控制台输出流std::cout,那么您得到的十六进制值是正确的
T.C.已经声明;它返回第一个索引的地址。这就是为什么C/C++数组和指针是相似的(它们不一样,但几乎彼此相似)。数组的主要区别在于,它们的大小是恒定的,必须在编译时知道大小,并且在创建一个较大的新数组时,不必将内容存储到临时变量中,就不能动态地更改大小,然后将所有数据复制到新数组中,然后清理旧数组。对于指针,它们不会以这种方式运行,可以使用new在堆上动态分配指针,但当该变量不再用于防止内存泄漏时,也必须删除指针,如果指针已被删除,并且有东西试图访问它,则内存地址不再有效且不属于调用者,这些异常通常被视为未经处理的异常、堆损坏等,会使程序崩溃。当您尝试将数组索引到超出其边界时,数组也是如此
#include <iostream>
int main() {
// Arrays Are 0 Indexed
int iArray[3] = { 1, 2, 3 };
// If you try to do this to access the last element
std::cout << iArray[3] << std::endl; // Program Will Crash
// Since Arrays Are 0 Indexed And The Size Of The Array Is 3
// The 3rd Value Is Indexed At 2, And By Trying To Index Location Of Array
// At 3, This Memory Doesn't Belong To You, And It Is Undefined Behavior.
// This Memory Could Contain Nothing, Random Data, Or Even Data That Belongs To
// Something Else Which If Changed Can Even Cause System Failure
return 0;
}
#包括
int main(){
//数组的索引为0
int-iArray[3]={1,2,3};
//如果尝试执行此操作以访问最后一个元素
STD:太长了,读不下去了。克里斯,你好。你能不能解释一下这是什么?@ TC,你的评论使我们想起了一些常见的问题。我很遗憾,我没有遇到过这个问题。他得到了,KUOSO。瓦泽尔,但是对于TL,T.C.博士,你应该把这张纸条移到为什么要打印地址而不是字符串。因为那部分就是答案。@user4581301我不知道,对我来说答案是“它不应该打印任何东西,因为它不会编译”。。。
int main() {
int iArray[]; // Compiler Error
int iArray[] = { 4, 3, 2, 1, 6 }; // Okay and is the same as
int iArray[5]; // Okay
iArray[0] = 4;
iArray[1] = 3;
iArray[2] = 2;
iArray[3] = 1;
iArray[4] = 6;
return 0;
#include <iostream>
int main() {
// Arrays Are 0 Indexed
int iArray[3] = { 1, 2, 3 };
// If you try to do this to access the last element
std::cout << iArray[3] << std::endl; // Program Will Crash
// Since Arrays Are 0 Indexed And The Size Of The Array Is 3
// The 3rd Value Is Indexed At 2, And By Trying To Index Location Of Array
// At 3, This Memory Doesn't Belong To You, And It Is Undefined Behavior.
// This Memory Could Contain Nothing, Random Data, Or Even Data That Belongs To
// Something Else Which If Changed Can Even Cause System Failure
return 0;
}