Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/128.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 指针数组混淆_C++_Arrays_Pointers_Stream_Byte - Fatal编程技术网

C++ 指针数组混淆

C++ 指针数组混淆,c++,arrays,pointers,stream,byte,C++,Arrays,Pointers,Stream,Byte,好的,根据我对指针数组的理解: 数组被认为等同于常量指针,因为它指向它的每个索引元素。此外,指向数组的指针相当于第一个索引的地址 好吧,既然这是有效的: int test[] = {1, 2, 3} int * point = test; cout << test + 2; //gives the address of the 3rd element test[2] int test[]={1,2,3} int*点=测试; 不能 相当于 char arr[] = {'h', '

好的,根据我对指针数组的理解:

数组被认为等同于常量指针,因为它指向它的每个索引元素。此外,指向数组的指针相当于第一个索引的地址

好吧,既然这是有效的:

int test[] = {1, 2, 3}

int * point = test;

cout << test + 2; //gives the address of the 3rd element test[2]
int test[]={1,2,3}
int*点=测试;
不能

相当于

char arr[] = {'h', 'e', 'l', 'l', 'o', '\0'};
唯一的区别是字符串文字(如
“hello”
)的含义,而不是
字符数组的处理方式与其他内置类型的数组的处理方式不同

接下来,当您将
char*
传递给
std::cout

std::cout << pointer + 3 << endl;
std::cout之所以会发生这种情况,是因为在C语言中(以及由于历史原因,在C++中),指向字符的指针在大多数情况下被认为是指向以字符
NUL
结尾的字符序列的指针(写为
'\0'

这样,如果你有一个C++函数接受一个<代码>:ST::String ,你也可以传递一个指向char的指针,因为语言假定你想从C字符串(即,由代码> NUL< /CUD>终止的字符序列)和标准的C++字符串进行隐式转换。 输出操作符也执行相同的操作,因此在发送

test+2
时,它假定您确实希望从那里开始输出C字符串

它指向它的每个索引元素

您忘记了数组的一些重要内容,它们是一种数据结构,被授权在内存中的相邻区域中分配,并且每个记录都包含相同类型的数据。考虑到数组的定义是您真正需要知道的唯一事情,即数组的开始位置和结束时间,您不需要指向数组中的每一条记录,因为您可以确定它将位于任何给定数组的开始和结束之间

你举例

int test[] = {1, 2, 3}

int * point = test;

cout << test + 2; //gives the address of the 3rd element test[2]

这是所有你需要知道的关于指针运算的知识,记住,没有自动边界检查,一切都在你的手中

指针不是数组,反之亦然。它们是不同的。看哪一个详细阐述了@chris的观点。好吧,为什么在字符数组的情况下,不输出变量的地址,就像它输出整数一样?@AndyCarter我补充了一个解释。+1提到了C字符串文字的历史C符合性规则
char arr[] = {'h', 'e', 'l', 'l', 'o', '\0'};
std::cout << pointer + 3 << endl;
int test[] = {1, 2, 3}

int * point = test;

cout << test + 2; //gives the address of the 3rd element test[2]
#include <stdio.h>

int main()
{
  int k[] = {34,765,23,5,7,123,8,99};
  int j = 3;

  printf("\nfirst print is  %d \n",k[j]);
  printf("\nsecond print is %d \n",j[k]);

  return(0);
}