简体   繁体   English

指针和字符串文字

[英]pointers and string literals

Many a times I have seen the following statements: 很多次我看到以下陈述:

char* ch = "Hello"
cout<<ch;

The output I get is " Hello ". 我得到的输出是“ 你好 ”。 I know that ch points to the first character of the string "Hello" and that "Hello" is a string literal and stored in read only memory. 我知道ch指向字符串“Hello”的第一个字符,而“Hello”是字符串文字并存储在只读存储器中。 Since, ch stores the address of the first character in the string literal, so shouldn't the statement, 因为,ch将第一个字符的地址存储在字符串文字中,所以不应该是语句,

cout<<ch;

give the output " the address of a character " because, it is a pointer variable? 给输出“ 一个字符的地址 ”因为,它是一个指针变量? Instead it prints the string literal itself. 相反,它打印字符串文字本身。 Moreover, if I write this, 而且,如果我写这个,

ch++;
cout<<ch;

It gives the output, " ello ". 它给出了输出“ ello ”。 And similarly, it happens with more consecutive ch++ statements too. 同样地,它也发生在更多连续的ch ++语句中。

can anybody tell me, why does it happen? 任何人都可以告诉我,为什么会这样?
Btw, I have seen other questions related to string literals, but all of them address the issue of "Why can't we do something like *ch='a'? 顺便说一句,我已经看到了与字符串文字有关的其他问题,但所有这些问题都解决了“为什么我们不能做像* ch ='a'这样的问题?
EDIT : I also want to ask this in reference to C, it happens in C too, if I type, 编辑:我也想问这个参考C,它也发生在C中,如果我输入,

printf("%s",ch);

Why? 为什么?

There's overloaded version of operator<< operator<<的重载版本

ostream& operator<< (ostream& , const char* );

This overload is called whenever you use something like:- 只要您使用以下内容,就会调用此重载: -

char *p = "Hello";
cout << p;

This overload defines how to print that string rather than print the address. 此重载定义了如何打印该字符串而不是打印地址。 However, if you want to print the address, you can use, 但是,如果要打印地址,可以使用,

cout << (void*) p;

as this would call another overload which just prints the address to stream:- 因为这会调用另一个重载,只是打印地址流: -

ostream& operator<< (ostream& , void* );

Printing a char* in C++ gives you the string rather than a pointer because that's how the language is defined, and in turn because that's what makes sense and is useful. 在C ++中打印char *会为您提供字符串而不是指针,因为这就是语言的定义方式,反过来因为这是有意义的,也是有用的。 We rarely need to print the address of a string, after all. 毕竟,我们很少需要打印字符串的地址。 In terms of implementation, it can be achieved by overloading an operator: 在实现方面,可以通过重载运算符来实现:

std::ostream& operator <<(std::ostream&, const char*);

You may find something like that in your standard library implementation, but it will actually be a template: 您可以在标准库实现中找到类似的东西,但它实际上是一个模板:

template <typename CharT>
std::basic_ostream<CharT>& operator <<(std::basic_ostream<CharT>&, const CharT*);

And it will probably have even more template parameters. 它可能会有更多的模板参数。

As for why ch++ gives you a string starting from the second character, well, that's because incrementing a pointer advances it to the next element in the "array" it points to. 至于为什么ch++给你一个从第二个字符开始的字符串,那就是因为递增一个指针使它前进到它所指向的“数组”中的下一个元素。

To answer your edited question, regarding printf() in c , it is determined by the format specifier supplied with printf() . 要回答您编辑的问题,关于c printf() ,它由printf()提供的格式说明符确定。 Check the following code 请检查以下代码

#include <stdio.h>
#include <stdlib.h>

int main()
{

    char * p = "Merry Christmas";

    printf("[This prints the string] >> %s\n", p);
    printf("[This prints the pointer] >> %p\n", p);

    return 0;
}

Output: 输出:

[sourav@broadsword temp]$ ./a.out 
[This prints the string] >> Merry Christmas
[This prints the pointer] >> 0x80484a0
[sourav@broadsword temp]$

for the same variable p , %s is used to print the string, whereas %p is used to print the p pointer itself. 对于相同的变量p%s用于打印字符串,而%p用于打印p指针本身。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM