简体   繁体   English

const char* 与 char* (C++)

[英]const char* vs char* (C++)

For the following program:对于以下程序:

int DivZero(int, int, int);

int main()
{
    try {
        cout << DivZero(1,0,2) << endl;
    }
    catch(char* e)
    {
        cout << "Exception is thrown!" << endl;
        cout << e << endl;
        return 1;
    }
    return 0;
}

int DivZero(int a, int b, int c)
{
    if( a <= 0 || b <= 0 || c <= 0)
        throw "All parameters must be greater than 0.";
    return b/c + a;
}

Using char* e will give使用 char* e 将给出

terminate called after throwing an instance of 'char const*'在抛出 'char const*' 的实例后调用终止

According to C++ Exception Handling , the solution is to use const char* instead.根据C++ 异常处理,解决方案是改用const char*

Further reading from function (const char *) vs. function (char *) said that进一步阅读function (const char *) vs. function (char *)

The type of "String" is char*', not const char*' “String”的类型是char*', not const char*'

(this is a C discussion I think...) (我认为这是一个 C 讨论......)

Additional reading on Stack Overflow char* vs const char* as a parameter tells me the difference. Stack Overflow char* vs const char* 作为参数的附加阅读告诉我区别。 But none of them address my questions:但他们都没有解决我的问题:

  1. It seems like both char* and string* have limit on the numbers of characters. char*string*似乎都对字符数有限制。 Am I correct?我对么?
  2. How does adding the keyword const to char* eliminates that limit?将关键字const添加到char*如何消除该限制? I thought the only purpose of const is to set a flag that said "unmodifiable".我认为const的唯一目的是设置一个“不可修改”的标志。 I understand that const char* e means " the pointer which points to unmodifiable char type".我知道const char* e 的意思是“指向不可修改的 char 类型的指针”。

The solution to that error is to use const char* e .该错误的解决方案是使用const char* e

Even const string* e doesn't work.甚至 const string* e 也不起作用。 (just for the sake of testing...) (只是为了测试......)

Can anyone explain, please?谁能解释一下,好吗? Thank you!谢谢!

By the way, I am on Ubuntu, compiled by GCC, on Eclipse.顺便说一句,我在 Ubuntu 上,由 GCC 编译,在 Eclipse 上。

The email you linked to about "String" is wrong (and confusing).您链接到的关于“字符串”的 email 是错误的(并且令人困惑)。

Basically:基本上:

char* is a pointer to an unbounded array of characters. char*是指向无界字符数组的指针。 Traditionally we consider such an array to be a C-string if it contains a set of valid characters followed by a \0 .传统上我们认为这样一个数组是一个 C 字符串,如果它包含一组有效字符,后跟一个\0 There's no limit to the size of the array.数组的大小没有限制。

const char* is a pointer to an unbounded array of immutable characters. const char*是指向不可变字符的无限数组的指针。

string* is a pointer to a std::string object, and is entirely different. string*是指向std::string object 的指针,并且完全不同。 This is a smart object that encapsulates a string.这是一个封装字符串的智能 object。 Using std::string instead of C-strings can make your life loads easier, even though they've got some rough edges and a lot of nasty gotchas;使用std::string代替 C 字符串可以让你的生活更轻松,即使它们有一些粗糙的边缘和很多讨厌的陷阱; they're well worth looking into, but they're not relevant to the question.它们非常值得研究,但与问题无关。

"String" is a special expression that returns a const char* pointing at the specific C-string (note: this is not actually true, but it's a simplification that lets me answer the question concisely). "String"是一个特殊的表达式,它返回一个指向特定 C 字符串的const char* (注意:这实际上不是真的,但它是一种简化,可以让我简洁地回答这个问题)。

A char* can be automatically cast to a const char* , but not vice versa. char*可以自动转换为const char* ,反之则不行。 Note that old C++ compilers had a special exception to the type rules to let you do this:请注意,旧的 C++ 编译器对类型规则有一个特殊例外,可让您执行此操作:

char* s = "String";

...without producing a type error; ...不产生类型错误; this was for C compatibility.这是为了与 C 兼容。 Modern C++ compilers won't let you do it (such as recent gccs).现代 C++ 编译器不会让您这样做(例如最近的 gcc)。 They require this:他们要求这样做:

const char* s = "String";

So.所以。 The problem here is that you've got:这里的问题是你有:

throw "All parameters must be greater than 0.";

...but then you're trying to catch it with: ...但是你试图抓住它:

catch(char* e)

This doesn't work, because the throw is throwing a const char* , which can't be cast to the type specified in the catch, so it isn't getting caught.这不起作用,因为 throw 正在抛出一个const char* ,它不能转换为 catch 中指定的类型,所以它不会被捕获。

This is why changing the catch to:这就是为什么将捕获更改为:

catch (const char* e)

...makes it work. ...使其工作。

Why are you throwing and catching strings anyway?你为什么还要扔和接绳子呢?

You should throw and catch exceptions, eg std::runtime_error您应该抛出并捕获异常,例如std::runtime_error

The answer to your question is that whenever you insert a string in quotes in the code it returns a null terminated const char*您的问题的答案是,每当您在代码中的引号中插入一个字符串时,它都会返回一个 null 终止的 const char*

The reason your code doesn't work as above is because it's the wrong type, so that catch, isn't catching what you're throwing.你的代码不能像上面那样工作的原因是因为它是错误的类型,所以 catch 并没有捕捉到你抛出的东西。 You're throwing a const char*.你正在抛出一个 const char*。

There is no limit to the number of characters in a char array beyond the size of your stack/heap. char 数组中超出堆栈/堆大小的字符数没有限制。 If you're referring to the example you posted, that person had created a fixed size array, so they were limited.如果您指的是您发布的示例,则该人创建了一个固定大小的数组,因此它们受到限制。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM