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

查看:310
本文介绍了const char * vs char *(C ++)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下程式:

  int DivZero(int,int,int); 

int main()
{
try {
cout< DivZero(1,0,2)< endl
}
catch(char * e)
{
cout< 抛出异常! << endl
cout<< e - < endl
return 1;
}
return 0;
}

int DivZero(int a,int b,int c)
{
if(a <= 0 || b <= 0 || c <= 0)
throw所有参数必须大于0。
return b / c + a;
}

使用char * e将给予


在抛出
'char const *'实例后调用终止


根据 C ++异常处理,解决方案是改用 const char * 。 p>

进一步阅读 function(const char *)vs. function(char *)表示


String的类型 char *',not
const char *'


是一个C讨论我认为...)



Stack Overflow上的额外阅读char* vs const char *作为参数告诉我的区别。但没有一个解决我的问题:


  1. 看起来像 char * string * / strong>对字符数有限制。

    char * 如何将关键字 const
    添加到 char * 我认为 const 的唯一目的是设置一个表示不可修改的标志。我了解 const char * e表示指向不可修改的char类型的指针。

该错误的解决方案是使用 const char * e



即使常数字符串* e不起作用。 (只是为了测试...)



任何人都可以解释一下吗?谢谢!



顺便说一下,我在Eclipse上的GCC编译的Ubuntu上。



你应该抛出和捕获异常,例如: std :: runtime_error



你的问题的答案是,每当你在代码中插入一个带引号的字符串时,它返回一个空的终止的const char *



你的代码不能像上面那样工作的原因是因为它是错误的类型,所以catch,不捕捉你抛出的东西。你抛出一个const char *。



除了堆栈/堆的大小之外,char数组中的字符数没有限制。如果你指的是你发布的例子,那个人创建了一个固定大小的数组,所以他们是有限的。


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

terminate called after throwing an instance of 'char const*'

According to C++ Exception Handling , the solution is to use const char* instead.

Further reading from function (const char *) vs. function (char *) said that

The type of "String" is char*', not const char*'

(this is a C discussion I think...)

Additional reading on Stack Overflow char* vs const char* as a parameter tells me the difference. But none of them address my questions:

  1. It seems like both char* and string* have limit on the numbers of characters. Am I correct?
  2. How does adding the keyword const to char* eliminates that limit? I thought the only purpose of const is to set a flag that said "unmodifiable". I understand that const char* e means " the pointer which points to unmodifiable char type".

The solution to that error is to use const char* e.

Even const string* e doesn't work. (just for the sake of testing...)

Can anyone explain, please? Thank you!

By the way, I am on Ubuntu, compiled by GCC, on Eclipse.

解决方案

Why are you throwing and catching strings anyway?

You should throw and catch exceptions, e.g. 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*

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. You're throwing a const char*.

There is no limit to the number of characters in a char array beyond the size of your stack/heap. If you're referring to the example you posted, that person had created a fixed size array, so they were limited.

这篇关于const char * vs char *(C ++)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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