C++ char* 与 int*

Anshuman Jagtap

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 概述了另一种解决方案。

丹尼尔·戴

正如已经指出的那样, achar *和 an之间没有区别(当然,指向类型除外)int *不同的输出,你所看到的是,由于非成员重载operator<<造成了特殊的处理charchar *

要“查看”指针,您可以将指针转换为void const *或直接调用成员函数(位于 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

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

在C#中从char int直接转换为char

来自分类Dev

为什么在C语言中(char)+(char)=(int)?

来自分类Dev

C如何在char数组中解析int和char?

来自分类Dev

在C ++中将char数组转换为int

来自分类Dev

在C中将int转换为char?

来自分类Dev

在C中将char数组转换为int

来自分类Dev

目标C的unsigned char为负int

来自分类Dev

将char *转换为int * C ++

来自分类Dev

将int转换为char C公式

来自分类Dev

C-将char乘以int

来自分类Dev

在C ++中从int *转换为char *

来自分类Dev

在C中将int转换为char

来自分类Dev

将int存储在C的char数组内部

来自分类Dev

C ++ int和char数组地址

来自分类Dev

将char **转换为int C ++

来自分类Dev

从C中的int输入解析char

来自分类Dev

在C中将int转换为char的问题

来自分类Dev

目标C的unsigned char为负数int

来自分类Dev

将int转换为Char c#?

来自分类Dev

python:TypeError:%c需要int或char

来自分类Dev

在C的char数组中添加int

来自分类Dev

从std :: vector <int>转换为char [] C ++

来自分类Dev

无效*到C中的char或int

来自分类Dev

输入int后的c ++输入char数组

来自分类Dev

C ++将Ascii Int转换为Char至Int

来自分类Dev

在C ++中使用int main(int argc,char ** argv)

来自分类Dev

int main(int argc,char ** argv)与c中的<stdin> stdout

来自分类Dev

在C中传递时,'Char'被提升为'int'

来自分类Dev

如何理解“ C ++允许sizeof(char *)!= sizeof(int *)”?