定义常量指针常量字符串 [英] Defining const pointer to a const string

查看:165
本文介绍了定义常量指针常量字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

乌尔里希Drepper的Readed沼泽和遇到2项,看起来像conficting。

Readed bog of Ulrich Drepper and come across 2 entries that looks like conficting.

第一个 (适用于全球空间字符串)乌尔里希指出,该字符串应该定义为:

In the first one (string in global space) Ulrich states that the string should be defines as:

const char _pcre_ucp_names[] = "blabla";

虽然已在 第二个 (字符串函数),他认为它应该被声明为:

while already in second one (string in function) he argues it should be declared as:

static const char _pcre_ucp_names[] = "blabla";

您能否解释一下什么是更好的名字来declate一个字符串?

Can you explain what is the better name to declate a string?

UDP:

首先我删除C ++标签 - 这个问题是有效的对C和C ++的。所以,我不认为这解释类/函数什么静态方法答案/文件范围是相关的。

First of all I removed C++ tag - this question is valid for C as well for C++. So I don't think answers which explain what static means in class/function/file scope is relevant.

在回答之前,请阅读文章。这些文章涉及内存使用情况 - 实际数据存储(以.RODATA或.data段),执行字符串应该被重新定位(如果我们谈论的Unix / Linux共享对象),是否有可能改变串或没有。

Read the articles before answering. The articles deal about memory usage - where the actual data is stored (in .rodata or in .data section), do the string should be relocated (if we're talking about unix/linux shared objects), is it possible to change the string or not.

UDP2
第一个它说,对于以下形式的全局变量:

UDP2 In first one it's said that for global variable following form:

(1) const char *a = "...";

是不太好的比

(2) const char a[] = "..."

为什么呢?我始终认为,(1)比较好,因为(2)实际上复制我们分配给它,而(1)仅指向字符串我们分配的字符串。

Why? I always thought that (1) is better, since (2) actually replicate the string we assign it, while (1) only points to string we assign.

推荐答案

这取决于 - 如果你需要的字符串,在项目中其他源文件可见,你的无法的声明它静态。如果你只需要从它的定义的文件访问它,那么你可能想使用静态

It depends—if you need the string to be visible to other source files in a project, you can't declare it static. If you only need to access it from the file where it's defined, then you probably want to use static.

你提到的博客文章说的是不同的东西,虽然

The blog post you mention was talking about something different, though:

#include <stdio.h>
#include <string.h>
int main(void)
{
  const char s[] = "hello"; /* Notice this variable is inside a function */
  strcpy (s, "bye");
  puts (s);
  return 0;
}

在这种情况下,静态意味着不同的东西:这将创建跨多个调用同样的功能仍然存在一个变量。他的其他例子说明一个全局变量,函数外。

In that case, static means something different: this creates a variable that persists across multiple calls to the same function. His other example showed a global variable, outside of a function.

编辑:

要澄清一下,因为你编辑你的问题,你不希望使用的原因为const char * A =串为您创建一个额外的可写指针。这意味着,虽然你不能改变字符串的字符,你仍然可以使指针指向一个完全不同的字符串。见下图:

To clarify, since you edited your question, the reason you don't want to use const char *a = "string" is you create an extra writable pointer. This means that, while you can't change the characters of the string, you can still make the pointer point to an entirely different string. See below:

const char *hello = "hello";

int main( int argc , char const *argv[] )
{
    hello = "goodbye";
    puts(hello);
    return 0;
}

这个例子编译和运行。如果你好应该是不变的,这肯定不是你想要的。当然,你也可以解决这个问题写这样的:

That example compiles and runs. If hello is supposed to be constant, this is surely not what you want. Of course, you can also get around this by writing this:

const char * const hello = "hello";

您仍然有两个变量,你只需要一个,但 - 你好是一个指向字符串常量,其中,如果它是一个数组中没有的方式,额外的指针

You still have two variables where you only needed one though -- hello is a pointer to a string constant, where if it's an array there isn't that extra pointer in the way.

这篇关于定义常量指针常量字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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