【问题标题】:C++ Char Array pointer behavior vs string Array pointerC++ Char 数组指针行为与字符串数组指针
【发布时间】:2017-09-10 23:35:17
【问题描述】:

第一个问题的 C++ 菜鸟。使用 Code:Blocks 16.01 GNU GCC 编译器。 提前致谢。代码;

#include <iostream>
using namespace std; 

int main(){
    char charArr[]="Hello";
    cout<<charArr<<endl;  //outputs Hello.

    string strArr[]={"Hello", "Stack", "overflow"};
    string *pStrArr=strArr; //pointer to strArr; same as &strArr[0].
    cout<<*pStrArr<<endl; //Derreferencing pointer , outputs Hello

    char charArr1[]="Hello";
    char *pCharArr1=charArr1;  /*pointer to charArr1.(charArr cout was Hello, not H, therefore i assumed we are storing in memory Hello);*/
    cout<<*pCharArr1<<endl;  /*dereferencing, outputs H, not Hello as i expected. */

    return 0; 
}

观察; charArr 输出 Hello,因此我假设创建指针并取消引用它应该输出 Hello;实际输出是 H,这似乎与在字符串 Array 上观察到的行为不一致,而第一个元素既被指向又被取消引用。

问题是: 显然我无法理解 char 数组。我希望以(尽可能)外行的方式对上述内容进行解释。

PS:确实使用了搜索功能并与鸭子交谈。谢谢你的时间。 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ +++++++++++++++++++++++++++ 在所有答案之后,我意识到实际问题应该是为什么第 2 行和第 4 行会产生不同的输出,strArr 是内存地址(表现为指针),而 charArr 输出数组内容。

string strArr[]= {"hello","world","how", "are","you"};
cout<<strArr<<endl;//outputs 0x28fedc.

char charArr[]="Hello";
cout<<charArr<<endl; // outputs hello

谢谢

【问题讨论】:

  • 您似乎知道char *pCharArr1=charArr1; 使pCharArr1 等于&amp;charArr1[0]。因此,*pCharArr1 等同于 charArr1[0] - 单个字符。如果您打印cout &lt;&lt; charArr1[0],您还会看到一个字符H。我不知道你为什么对这种行为感到惊讶。
  • 不要使用原始数组! “PS:确实使用了搜索功能并与鸭子交谈。感谢您的时间。”进一步与您的鸭子交谈!
  • 你需要一个good c++ book
  • 这是错误的鸭子:youtube.com/watch?v=32VLnTKz0CI。如果你能听懂他在说什么,请忽略它。

标签: c++ arrays string pointers implicit-conversion


【解决方案1】:

在这段代码中sn-p

char charArr1[]="Hello";
char *pCharArr1=charArr1;  /*pointer to charArr1.(charArr cout was Hello, not H, therefore i assumed we are storing in memory Hello);*/
cout<<*pCharArr1<<endl;  /*dereferencing, outputs H, not Hello as i expected. */

对象pCharArr1 的类型为char *。它指向存储在数组charArr1 中的字符串"Hello" 的第一个字符。解除对指针的引用,您将获得char 类型的对象,即指针所指向的字符。是'H'这个角色。

在这段代码中sn-p

string strArr[]={"Hello", "Stack", "overflow"};
string *pStrArr=strArr; //pointer to strArr; same as &strArr[0].
cout<<*pStrArr<<endl; //Derreferencing pointer , outputs Hello

对象pStrArr 具有std::string * 类型并指向std::string 类型的对象。解除对指针的引用,您将获得一个类型为std::string 的对象,其中包含字符序列"Hello"。所以在这个声明中

cout<<*pStrArr<<endl; //Derreferencing pointer , outputs Hello

这个序列被输出了。

因此在此声明中

cout<<charArr<<endl;  //outputs Hello

charArr 的类型为 char *(数组指示符隐式转换为指向其第一个元素的指针)。

在此声明中

cout<<*pCharArr1<<endl;  /*dereferencing, outputs H, not Hello as i expected. */

*pCharArr1 的类型为 char

最后在这个声明中

cout<<*pStrArr<<endl; //Derreferencing pointer , outputs Hello

*pStrArr 的类型为 std::string

考虑到这些声明

char charArr1[]="Hello";
char *pCharArr1=charArr1;  /*pointer to charArr1.(charArr cout was Hello, not H, therefore i assumed we are storing in memory Hello);*/

这些语句的输出

cout<<CharArr1<<endl;  

cout<<pCharArr1<<endl;  

将相同,将等于输出字符串"Hello"

但是对于这些陈述

cout<<*CharArr1<<endl;  

cout<<*pCharArr1<<endl;  

只会输出一个字符'H'

在第一条语句中提到,数组指示符被隐式转换为指向表达式*CharArr1中第一个元素的指针

【讨论】:

  • 有道理 Vlad & Igor,所以看看 char charArr[]="Hello";作为等效的 char charArr[]={'H','e','l','l','o'} 我实际上指的是第一个字符,即 'H'...我猜我我正在努力理解(并将其表述为一个可理解的问题)是为什么 cout
  • @RookieCplusplus 这个声明 charArr[]="Hello";相当于下面的声明 charArr[]= { 'H', 'e', 'l', 'l', 'o', '\0' };;
  • @RookieCplusplus 每个带有
猜你喜欢
  • 2021-01-26
  • 2020-11-21
  • 2018-04-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多