什么是静态常量char * const的,静态为const char []的区别? [英] What is the difference between static const char * const and static const char []?

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

问题描述

可能重复:结果
  <一href=\"http://stackoverflow.com/questions/9460260/what-is-the-difference-between-char-a-string-and-char-p-string\">What为char之间的[] =串的区别;和char * p =串;

将数组版本分配数组的内存,所以一个100字节的字符串,将使用在静态数组的等截面和100 100字节,还是会只使用100字节总数是多少?和指针版本,将它分配为指针字的大小除了100字节的字符串,或将鼠标指针进行优化,以恒定的部分地址干脆?

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?

推荐答案

如果您使用的是常见的电脑,用 .RODATA 部分:

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;
}

它分配 100 +的sizeof(字符*)字节中的 .RODATA 部分。

#include <stdio.h>

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

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

它分配 .RODATA 第100条字节。

It allocates 100 bytes in the .rodata section.

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

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