char array []和char * array之间有什么区别? [英] What is the difference between char array[] and char *array?

查看:623
本文介绍了char array []和char * array之间有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  char hello 

这两个选项之间的区别是什么? ] = {'h','e','l','l','o','\0'};

char * hello =hello;

但是在正确的c ++中,第二个应该是:

  const char * hello =hello; 

在大多数情况下,我想你永远不会改变hello,但为什么不能

解决方案


char hello [ ] = {'h','e','l','l','o','\0'};


这会在可写存储器中创建一个6字节的数组(如果在函数内部,则在堆栈上,在数据段中,如果直接在全局范围或命名空间中),将被初始化


char * hello =hello;


hello是一个字符串,通常 >表示:




  • 将程序加载到内存并启动它的操作系统加载程序代码将复制文本hello\\ \\ 0从可执行映像插入某些已设置为只读的内存中,并且


  • 一个名为hello的独立变量你的程序中的任何大小的指针(例如4位用于32位应用程序,8位用于64位) - 将存在于堆栈(如果上面的行出现在函数内部)或可写存储器段(如果该行在全局或命名空间范围),并且地址


  • 您可以更改 hello 指向别的地方(例如另一个语言的等效文本),但通常不应该尝试改变上面代码指向的字符串文字 hello ...见下文。





第二个应为:



const char * hello =hello;


是的 - 这是好多了。为了向后兼容性,C ++历史上允许非 - const 指向字符串字面量,但是实际上修改它们不能保证是可能的。有些编译器总是,或者可以(当命令行参数询问时)将字符串文字放在可写内存中。但是,即使它们是可写的,改变它们也会导致很多潜在的错误。假设你有这样的代码和编译器标志,以允许它编译:

  char * end =end 
if(mixed_case_mode)end [0] ='E';
if(s ==end)...;

然后一些完全不相关的代码...

  std :: cout< 这是< (x?start:end); 

...如果end的所有提及共享相同的内存,一个常见的和令人满意的优化。关闭这可能是相当浪费,虽然它允许这个hackery工作与较少的非预期的副作用对文本的不相关的使用,仍然很难推理代码和调试时, if(s = =end)实际上可能不会将 s 比较到end / p>

What is the difference between doing these two options, and why is the second type deprecated in C++?

char hello[] = {'h', 'e', 'l', 'l', 'o', '\0'};

char *hello = "hello";

But in correct c++ the second should be:

const char *hello = "hello";

For the most part I guess you are never going to change "hello", but why can't you change this memory if you wanted to?

解决方案

char hello[] = {'h', 'e', 'l', 'l', 'o', '\0'};

This creates an array of 6 bytes in writable memory (on the stack if this is inside a function, in a data segment if directly at global scope or inside a namespace), to be initialised with the ASCII codes for each of those characters in turn.

char *hello = "hello";

"hello" is a string literal, which typically means:

  • the OS loader code that loads your program into memory and starts it running will have copied the text "hello\0" from your executable image into some memory that will then have been set to be read only, and

  • a separate variable named "hello" - which is of whatever size pointers are in your program (e.g. 4 bytes for 32-bit applications, 8 for 64-bit) - will exist on the stack (if the line above appears inside a function) or in writable memory segment (if the line is at global or namespace scope), and the address of the former textual data will be copied into the hello pointer.

  • you can change hello to point somewhere else (e.g. to another language's equivalent text), but normally shouldn't try to change the string literal to which the above code points hello... see below.

But in correct c++ the second should be:

const char *hello = "hello";

Yes - that's much better. For backwards compatibility, C++ has historically allowed non-const pointers to string literals, but actually modifying them wasn't guaranteed to be possible. Some compilers always, or can optionally (when asked by command line arguments), put string literals in writable memory. But, even if they are writable, changing them leads to lots of potential for errors. Say you have code like this and compiler flags to allow it to compile:

char* end = "end";
if (mixed_case_mode) end[0] = 'E';
if (s == "end") ...;

Then some completely unrelated code that does this...

std::cout << "this is the " << (x ? "start": "end");

...might start printing "End" if all mentions of "end" shared the same memory - a common and desirable optimisation. Turning this off can be quite wasteful, and while it allows this hackery to work with less unintended side effects to unrelated uses of the text, it's still very hard to reason about the code and debug when if (s == "end") might not actually be comparing s to "end".

这篇关于char array []和char * array之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆