访问二维数组(整数和字符数组) [英] Accessing the 2-D array( integer and character array)
问题描述
请考虑以下代码:
char arr[3][10]={"white","red","blue"};
cout<<arr[2]; //output 1
int brr[3][3]={1,2,3,4,5,6,7,8,9};
cout<<brr[2]; //output 2
cout<<*brr[2];
输出1:白色,解引用后将w作为输出
Output 1 : white, on dereferencing it gives w as output
输出2:十六进制地址(0x28 .....)
Output 2 : hexadecimal address(0x28.....)
输出3:最后一行的第一个元素7
Output 3 : Last row first element 7
我很困惑为什么整数和字符数组的输出存在差异.
I am confused why there is difference in output in array of integers and characters.
推荐答案
When you supply a pointer it will use the std::ostream& operator<<( const void* value );
overload and output the address pointed at by value
.
区别在于 std :: ostream&运算符<<(std :: ostream& os,const char * s)
是一种特殊情况,它将取消引用 s
并输出 char 直到遇到
\ 0
.只要不遇到 \ 0
,它就会将 s
向前移动.
The difference is that std::ostream& operator<<(std::ostream& os, const char* s)
is a special case and it will dereference s
and output the char
s until a \0
is encountered. As long as no \0
is encountered, it'll step s
forward.
这篇关于访问二维数组(整数和字符数组)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!