【发布时间】:2015-10-06 15:24:17
【问题描述】:
我正在学习指针,这是我的代码。我定义了一个指向 char(实际上是字符串)*str 的指针和一个指向 int *a 的指针,它们的定义方式相同。我以为str和a都应该是一个地址,但是当我尝试输出str和&str、a和a和&a时,我发现str不是地址,它是字符串。 char *str 和 int *a 在str 和a 的类型上有什么区别?谢谢你。
#include<iostream>
#include<string>
using namespace std;
int main()
{
char *str = "FA";
cout << "str: " << str << endl;
cout << "&str: " << &str << endl;
int b = 5;
int *a = &b;
cout << "a: " << a << endl;
cout << "&a: " << &a << endl;
}
这是输出:
str: FA
&str: 0x7fff5a627280
a: 0x7fff5a62727c
&a: 0x7fff5a627288
【问题讨论】:
-
优先使用
const char *作为字符串字面量。