C ++ char * vs int *

时间:2017-05-27 07:09:43

标签: 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在下面概述了另一种解决方案。

2 个答案:

答案 0 :(得分:1)

您创建的指针已存储c字符串中每个字符的地址。输出问题是 std::cout operator<<处理c字符串的方式。它将继续读取内存中的下一个条目,直到它达到空\0个字符。这就是为什么每一行你都得到了整个c字符串中的剩余字符。

编辑:为了更好地了解正在发生的事情,请参阅Daniel Jour的回答。

答案 1 :(得分:1)

正如已经指出的那样,char *int *之间没有区别(当然除了指向类型)。您看到的不同输出是由于operator<< char导致char *void const *的特殊处理。

To&#34;见&#34;指针,您可以将指针强制转换为#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; } 或直接调用成员函数(the non member overloads):

string: Hello
casted: 0x2b1c038fbb7d
member: 0x2b1c038fbb7d

示例输出:

import tensorflow as tf
import numpy as np

c = tf.constant(np.random.rand(3, 2, 3, 6))
d = tf.norm(c, ord=2)

with tf.Session() as sess:
    print sess.run(d)