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

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

问题描述

阅读了Ulrich Drepper的博客,并找到了两个看起来像是困惑的条目。



第一个 (全局空间中的字符串)Ulrich说,字符串应该定义为:

  const char _pcre_ucp_names [] =blabla;已在   



< 15119.htmlrel =nofollow noreferrer>第二个
(函数中的字符串)他认为应该声明为:

  static const char _pcre_ucp_names [] =blabla; 

你能解释一个更好的名字来声明一个字符串吗?



UDP



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



在回答之前阅读文章。文章讨论内存使用 - 实际数据存储在(.rodata或.data部分),字符串应该重定位(如果我们在谈论unix / linux共享对象),是否可以更改字符串或不。



UDP2
第一个中,表示全局变量形式如下:

 (1)const char * a =... 

不如

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

为什么?我总是认为(1)更好,因为(2)实际上复制了我们分配的字符串,而(1)只指向我们分配的字符串。

解决方案

这取决于您是否需要该字符串对项目中的其他源文件可见,您不能声明 static 。如果你只需要从它定义的文件中访问它,那么你可能想使用 static



你提到的博客帖子说的是不同的东西:

  #include< stdio.h> 
#include< string.h>
int main(void)
{
const char s [] =hello; / *注意这个变量在函数里面* /
strcpy(s,bye);
puts(s);
return 0;
}

在此情况下, static 意味着不同的东西:这将创建一个持续存在于同一函数的多个调用中的变量。

p>为了澄清,由于你编辑了你的问题,你不想使用 const char * a =string的原因是你创建了一个额外的可写指针。这意味着,虽然你不能改变字符串的字符,你仍然可以使指针指向一个完全不同的字符串。见下面:

  const char * hello =hello 

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

该示例编译并运行。如果hello应该是恒定的,这肯定不是你想要的。当然,你也可以这样写:

  const char * const hello =hello 

你仍然有两个变量,你只需要一个 - hello是一个指向字符串常量的指针,如果它是一个数组,没有那个额外的指针。


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

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

UDP:

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.

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 In first one it's said that for global variable following form:

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

is less good than

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

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.

EDIT:

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.

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

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