char *与为const char *之间的区别? [英] Difference between char* and const char*?

查看:259
本文介绍了char *与为const char *之间的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么

之间的差

 的char *名称

指向一个常量字符串,以及

 为const char *名称


解决方案

 的char *名称

您可以更改至名称点,以及在它指向炭炭。

 为const char *名称

<击>您可以更改到名称点炭,但不能在修改它指向的字符。

修正::您可以改变指针,但炭到名称点(<一个href=\"https://msdn.microsoft.com/en-us/library/vstudio/whkd4k6a(v=vs.100).aspx\">https://msdn.microsoft.com/en-us/library/vstudio/whkd4k6a(v=vs.100).aspx,见示例)。在这种情况下,常量说明适用于字符,而不是星号。

根据MSDN的页面,<一个href=\"http://en.cp$p$pference.com/w/cpp/language/declarations\">http://en.cp$p$pference.com/w/cpp/language/declarations,在常量的前 * 是DECL说明符序列的一部分,而常量 * 是说明符的一部分。

一个声明符序列可以跟多个声明,这就是为什么为const char * C1,C2 声明 C1 为const char * C2 为const char

编辑:

从评论,你的问题似乎是问两个声明,当指针指向字符串文本之间的差异。

在这种情况下,您不该修改到名称点,因为它可能导致的未定义行为炭
(实现定义)字符串可以在只读存储器区域分配和用户程序不应该修改它,无论如何。任何试图这样做的结果是未定义的行为。

因此​​,在(与字符串的使用),这种情况下,唯一的区别是第二个声明为您提供了一个略占优势。编译器通常会给你的情况下,试图修改字符串在第二种情况下的文字警告。

在线样本实施例:

 的#include&LT;&string.h中GT;
诠释的main()
{
    字符* STR1 =字符串;
    为const char * str2的=字符串;
    炭源[] =样本字符串;    的strcpy(STR1,源); //没有警告或错误,只是未定义行为
    的strcpy(STR2,源); //编译器会发出警告    返回0;
}

输出:


  

CC1:警告被视为错误结果
  prog.c中:在函数'主':结果
  prog.c中:9:错误:传递的strcpy的参数1丢弃了指针目标类型的限定


注意编译器警告对于第二种情况,但不是第一个。

What's the difference between

char* name

which points to a constant string literal, and

const char* name

解决方案

char *name

You can change the char to which name points, and also the char at which it points.

const char* name

You can change the char to which name points, but you cannot modify the char at which it points.
correction: You can change the pointer, but not the char to which name points to (https://msdn.microsoft.com/en-us/library/vstudio/whkd4k6a(v=vs.100).aspx, see "Examples"). In this case, the const specifier applies to char, not the asterisk.

According to the MSDN page and http://en.cppreference.com/w/cpp/language/declarations, the const before the * is part of the decl-specifier sequence, while the const after * is part of the declarator.
A declaration specifier sequence can be followed by multiple declarators, which is why const char * c1, c2 declares c1 as const char * and c2 as const char.

EDIT:

From the comments, your question seems to be asking about the difference between the two declarations when the pointer points to a string literal.

In that case, you should not modify the char to which name points, as it could result in Undefined Behavior. String literals may be allocated in read only memory regions (implementation defined) and an user program should not modify it in anyway. Any attempt to do so results in Undefined Behavior.

So the only difference in that case (of usage with string literals) is that the second declaration gives you a slight advantage. Compilers will usually give you a warning in case you attempt to modify the string literal in the second case.

Online Sample Example:

#include <string.h>
int main()
{
    char *str1 = "string Literal";
    const char *str2 = "string Literal";
    char source[] = "Sample string";

    strcpy(str1,source);    //No warning or error, just Undefined Behavior
    strcpy(str2,source);    //Compiler issues a warning

    return 0;
}

Output:

cc1: warnings being treated as errors
prog.c: In function ‘main’:
prog.c:9: error: passing argument 1 of ‘strcpy’ discards qualifiers from pointer target type

Notice the compiler warns for the second case but not for the first.

这篇关于char *与为const char *之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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