C++ char* 与 int*

标签 c++ pointers char int

<分区>

char* 和 int* 有什么区别?

例如当我运行这段代码时:

#include<iostream>
using namespace std;


int main(){
  int a[3] = {10, 20, 30};
  int *ptr2a = &a[0];
  int i;
  char line[] = "Hello";
  char *ptr2line =  &line[0];

  for(i=0; i<3; i++){
    cout<<"Value of a["<<i<<"] is: "<<a[i]<<" same as "<<*(ptr2a+i);
    cout<<" Address is: "<<ptr2a+i<<" , same as "<<&a[i]<<endl;
  }

  for(i=0; i<strlen(line); i++){
    cout<<"Value of line["<<i<<"] is: "<<line[i]<<" same as "<<*(ptr2line+i);
    cout<<" Address is: "<<ptr2line+i<<" , same as "<<&line[i]<<endl;
  }

return 0;
}

我得到了以下输出:

Value of a[0] is: 10 same as 10 Address is: 0x7fff5bf29b1c , same as 0x7fff5bf29b1c
Value of a[1] is: 20 same as 20 Address is: 0x7fff5bf29b20 , same as 0x7fff5bf29b20
Value of a[2] is: 30 same as 30 Address is: 0x7fff5bf29b24 , same as 0x7fff5bf29b24
Value of line[0] is: H same as H Address is: Hello , same as Hello
Value of line[1] is: e same as e Address is: ello , same as ello
Value of line[2] is: l same as l Address is: llo , same as llo
Value of line[3] is: l same as l Address is: lo , same as lo
Value of line[4] is: o same as o Address is: o , same as o

当我改变这一行时:

cout<<" Address is: "<<ptr2line+i<<" , same as "<<&line[i]<<endl;

到:

cout<<" Address is: "<<&ptr2line+i<<" , same as "<<&line[i]<<endl;

我得到了这个输出:

Value of line[0] is: H same as H Address is: 0x7fff5054bad0 , same as Hello
Value of line[1] is: e same as e Address is: 0x7fff5054bad8 , same as ello
Value of line[2] is: l same as l Address is: 0x7fff5054bae0 , same as llo
Value of line[3] is: l same as l Address is: 0x7fff5054bae8 , same as lo
Value of line[4] is: o same as o Address is: 0x7fff5054baf0 , same as o

在这种情况下 地址 0x7fff5054bad0 对应于 'Hello' 的值 0x7fff5054bad8对应'ello'等。

创建存储字符串每个字符地址的指针的正确方法是什么?

决议

可视化指针的一种方法是改用 printf

printf("Value of line[%d] is: %c same as %c, Address is: %p, same as %p\n", i, line[i], *(ptr2line+i),ptr2line+i,&line[i]);

给出想要的输出

Value of line[0] is: H same as H, Address is: 0x7fff54ce6ade, same as 0x7fff54ce6ade
Value of line[1] is: e same as e, Address is: 0x7fff54ce6adf, same as 0x7fff54ce6adf
Value of line[2] is: l same as l, Address is: 0x7fff54ce6ae0, same as 0x7fff54ce6ae0
Value of line[3] is: l same as l, Address is: 0x7fff54ce6ae1, same as 0x7fff54ce6ae1
Value of line[4] is: o same as o, Address is: 0x7fff54ce6ae2, same as 0x7fff54ce6ae2

下面@Daniel Jour 概述了另一种解决方案。

最佳答案

正如已经指出的那样,char * 之间没有区别(当然,指向的类型除外)。和一个 int * .您看到的不同输出是因为 the non member overloadsoperator<<这导致对 char 的特殊处理和 char * .

要“查看”指针,您可以将指针转换为 void const *或者直接调用成员函数(live on ideone):

#include <iostream>

int main() {
    char const * string = "Hello";
    std::cout << "string: " << string << std::endl
        << "casted: " << static_cast<void const*>(string) << std::endl
        << "member: ";
    std::cout.operator<<(string);
    std::cout << std::endl;
}

示例输出:

string: Hello
casted: 0x2b1c038fbb7d
member: 0x2b1c038fbb7d

关于C++ char* 与 int*,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44214032/

相关文章:

c - Websocket key 哈希

c++ - C++ 程序使用的 C 库中的错误处理

c++ - 返回指向类的指针的函数

c++ - 如何在 C++11 类的位集中有效地支持 "sub-bitstrings"?

c++ - 如何从父级不是 QMainWindow 的 QWidget 访问 QMainWindow

c - 在C函数中对数组排序

c++ - 指针容器

带有从迭代器获取的指针参数的 C++ 模板函数给出错误

c - fgetc 不工作 C-重复返回相同的字符

c++ - char a[n][m] 和 char a[][m] 有区别吗?