何时使用const char *与何时使用为const char [] [英] When to use const char * and when to use const char []

查看:151
本文介绍了何时使用const char *与何时使用为const char []的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道他们是不同的,我知道他们是如何是不同的,我读了所有问题,我能找到的关于的char * VS 的char []

I know they are different, I know how they are different and I read all questions I could find regarding char* vs char[]

但是,所有这些问题的答案永远不会告诉他们的时候应该使用。

But all those answers never tell when they should be used.

所以我的问题是:

当你用

const char *text = "text";

和什么时候使用

const char text[] = "text";

是否有任何指引或规则?

Is there any guideline or rule?

作为一个例子,其中之一是更好

As an example, which one is better:

void withPointer()
{
    const char *sz = "hello";
    std::cout << sz << std::endl;
}

void withArray()
{
    const char sz[] = "hello";
    std::cout << sz << std::endl;
}

(我知道的std ::字符串也是一种选择,但我特别想知道关于字符指针/数组)

(I know std::string is also an option but I specifically want to know about char pointer/array)

推荐答案

两者都明显不同,对于启动:

Both are distinctly different, For a start:


  1. 第一个创建的指针。

  2. 第二个创建一个数组。

阅读关于更详细的解释:

Read on for more detailed explanation:

char text[] = "text"; 

创建一个数组,它是大到足以容纳字符串文本,包括其 NULL 终止。该阵列文本被初始化字符串文本。该阵列可以在稍后的时间修改。此外,数组的大小即使在编译时已知,所以 的sizeof 操作符可以用来确定其大小。

Creates an array that is large enough to hold the string literal "text", including its NULL terminator. The array text is initialized with the string literal "text".The array can be modified at a later time. Also, the array's size is known even at compile time, so sizeof operator can be used to determine its size.

char *text  = "text"; 

创建一个指针指向一个字符串文本。这比数组版本速度更快,,但字符串的指针指向不应更改,因为它坐落在一个只读实现定义的内存。在未定义行为修改这样的字符串结果

Creates a pointer to point to a string literal "text". This is faster than the array version, but string pointed by the pointer should not be changed, because it is located in an read only implementation defined memory. Modifying such an string literal results in Undefined Behavior.

在事实上C ++ 03日precates使用字符串字面量的无常量关键字。因此,声明应该是:

In fact C++03 deprecates use of string literal without the const keyword. So the declaration should be:

const char*text = "text";

另外,你的需要使用的strlen()的功能,而不是的sizeof 找到大小字符串因为的sizeof 运营商只会给你的指针变量的大小。

Also,you need to use the strlen() function, and not sizeof to find size of the string since the sizeof operator will just give you the size of the pointer variable.

取决于用法。


  • 如果您不需要做字符串的任何变化,使用指针的版本。

  • 如果您打算更改数据,使用数组版本。

编辑:这只是带给我的通告(评论)的OP寻求差异:

It was just brought to my notice(in comments) that the OP seeks difference between:

为const char文本[] 为const char *的文本

那么上面的不同点仍然适用,除了一个关于修改字符串。随着常量预选赛数组测试现在包含的类型元素的数组为const char 这意味着它们不能被修改。

Well the above differing points still apply except the one regarding modifying the string literal. With the const qualifier the array test is now an array containing elements of the type const char which implies they cannot be modified.

鉴于这种情况,我会选择在指针版的阵列版本,因为指针可以是(错误),很容易重新插拔另一个指针和该字符串可以通过产生一种UB另一个指针进行修改。

Given that, I would choose the array version over the pointer version because the pointer can be(by mistake)easily reseated to another pointer and the string could be modified through that another pointer resulting in an UB.

这篇关于何时使用const char *与何时使用为const char []的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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