Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

const char* vs char* (C++)

Tags:

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

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.

like image 866
CppLearner Avatar asked Jun 01 '11 22:06

CppLearner


People also ask

Why do we use const char * in C?

In C programming language, *p represents the value stored in a pointer and p represents the address of the value, is referred as a pointer. const char* and char const* says that the pointer can point to a constant char and value of char pointed by this pointer cannot be changed.

What is the difference between char * C and char * C?

The main difference between them is that the first is an array and the other one is a pointer. The array owns its contents, which happen to be a copy of "Test" , while the pointer simply refers to the contents of the string (which in this case is immutable). Save this answer.

What does const char * means?

const char *ptr : This is a pointer to a constant character. You cannot change the value pointed by ptr, but you can change the pointer itself. “const char *” is a (non-const) pointer to a const char.

What is the difference between const char * and string?

string is an object meant to hold textual data (a string), and char* is a pointer to a block of memory that is meant to hold textual data (a string). A string "knows" its length, but a char* is just a pointer (to an array of characters) -- it has no length information.


2 Answers

The email you linked to about "String" is wrong (and confusing).

Basically:

char* is a pointer to an unbounded array of characters. Traditionally we consider such an array to be a C-string if it contains a set of valid characters followed by a \0. There's no limit to the size of the array.

const char* is a pointer to an unbounded array of immutable characters.

string* is a pointer to a std::string object, and is entirely different. This is a smart object that encapsulates a string. 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; 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).

A char* can be automatically cast to a const char*, but not vice versa. Note that old C++ compilers had a special exception to the type rules to let you do this:

char* s = "String";

...without producing a type error; this was for C compatibility. Modern C++ compilers won't let you do it (such as recent gccs). 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.

This is why changing the catch to:

catch (const char* e)

...makes it work.

like image 195
David Given Avatar answered Sep 19 '22 19:09

David Given


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.

like image 41
Salgar Avatar answered Sep 19 '22 19:09

Salgar