What is the difference between static const char * const and static const char []? [duplicate]

我怕爱的太早我们不能终老 提交于 2019-12-24 07:05:58

问题


Possible Duplicate:
What is the difference between char a[] = “string”; and char *p = “string”;

Will the array version allocate the array memory, so a 100 byte string will use 100 bytes on the constant section and 100 on the static array, or will it use only 100 bytes total? And the pointer version, will it allocate the word size for the pointer besides the 100 bytes of the string, or will the pointer be optimized to the constant section address altogether?


回答1:


If you use a common computer, with a .rodata section:

#include <stdio.h>

static const char *s = /* string of 100 characters */;

int main(void)
{
  puts(s);
  return 0;
}

It allocates 100 + sizeof(char *) bytes in the .rodata section.

#include <stdio.h>

static const char s[100] = /* string of 100 characters */;

int main(void)
{
  puts(s);
  return 0;
}

It allocates 100 bytes in the .rodata section.



来源:https://stackoverflow.com/questions/14294065/what-is-the-difference-between-static-const-char-const-and-static-const-char

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!