Jump to content
  • Advertisement
Sign in to follow this  
LAURENT*

can anyone explain const char*, char, strings, char array

This topic is 1028 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm sorely confused.

 

My compiler keeps telling me this function's argument only accepts "cons char*". I gave it a char, a string, a char array, a function that return a char. I really don't know what I'm dealing with. Can anyone explain what I have to give this function and compare and contrast these 3 things.

Edited by LAURENT*

Share this post


Link to post
Share on other sites
Advertisement

char is a single character, like 'a', 'B' or '1'. Can internally be any of 256 values (on most platforms where it is 8 bits, some exotic ones use another bit-count).

 

char[10] is an array of 10 characters stored sequentially in memory.

 

char* is a pointer which holds the address in memory of a character, for example it can point at a single 'char' variable, or it can point at the address where a sequential array begins.

const char* is such a pointer that is declared constant, which means that any function accepting it promises not to change the values stored in memory at that address. Read-only access to a string is usually the meaning.

 

std::string is a class that internally holds a memory-buffer of many characters, and has methods to manipulate them.

 

 

For string-functions that take const char* it is usually appropriate to pass them string.c_str(), as the c_str() method returns a pointer to an address in memory where the string characters are stored sequentially, + it guarantees to end that sequence with a null-character, which means that a 'char' with value 0 will be stored at the end of the sequence. Such a null-terminator is used by many string functions to determine where a string ends.

Share this post


Link to post
Share on other sites

Thanks, I think I get it now. Even if it's pointing to a char it's still a pointer. I was wrong to make the assumption it would accept. This was a big help toward my main goal. Again thanks.

Share this post


Link to post
Share on other sites

As a side note.

const is a good subject to explore and I believe you should get into the habit of making your code const aware, part of this is const functions in a class

 

In the following the function CalcSomeValue is tagged const, which means that it does not change the state of MyClass, it is a read only function. Been a while since I worked in C++ but I believe this is compiler checked.

 

The effect of this is that if you have a const MyCLass* you are only able to call const functions on the object as you have a const object.

class MyClass
{
public:
	int CalcSomeValue() const
	{
		// code that returns something
	}
};

Cost in C++, unlike C# and some other languages, is a subtle keyword that is worth learning. It provides lots of semantic implications in function contracts that you need to understand as you pull in 3rd party libraries etc as you will encounter it a lot :)

Share this post


Link to post
Share on other sites


For the other 1%, where the function is expecting char* and forgot to const-qualify it, you'll have to vomit up a const_cast.

 

I only ever had to use const_cast, when I used some badly designed or outdated libraries (looking at you, FreeImage!). I think replacing the library, or if it is your own code, fixing it, is the right answer. Beginners should not even be told about const_cast in my opinion, it'll just cause trouble somewhere down the road.

Share this post


Link to post
Share on other sites
If I'm forced to pass a char* due to a badly designed library, I prefer to do this than fall back on a const_cast, unless there is a good reason to avoid the additional overhead:
 
void badMethod(char *s);

void f(const char *s)
{
    std::vector<char> v(s, s + strlen(s) + 1); // +1 to ensure the null terminator is added
    badMethod(v.data());
}
const_cast is undefined behaviour if it is invoked on an entity that was originally declared as const, since the compiler may have decided to locate it in read-only memory. Edited by Aardvajk

Share this post


Link to post
Share on other sites

I only ever had to use const_cast, when I used some badly designed or outdated libraries (looking at you, FreeImage!).


That's what I'm talking about: the 1% is badly designed/outdated libraries. Unfortunately, I think one of the improperly non-const char* functions is inherited from the C standard library, though I can't remember what function.

I think replacing the library, or if it is your own code, fixing it, is the right answer. Beginners should not even be told about const_cast in my opinion, it'll just cause trouble somewhere down the road.

Absolutely!

Share this post


Link to post
Share on other sites

If I'm forced to pass a char* due to a badly designed library, I prefer to do this than fall back on a const_cast, unless there is a good reason to avoid the additional overhead:
 

void badMethod(char *s);

void f(const char *s)
{
    std::vector<char> v(s, s + strlen(s) + 1); // +1 to ensure the null terminator is added
    badMethod(v.data());
}
const_cast is undefined behaviour if it is invoked on an entity that was originally declared as const, since the compiler may have decided to locate it in read-only memory.

 

Wouldn't this work?

void badMethod(char *s);

void f(const char *s) // but likely the parameter will have to leave out const in its declaration, to not be a corrupted declaration?
{
    char* memofstr=s; 
    badMethod(memofstr);
}

I understand only that const at defintion makes compiler to warn/error you out if the variable is left-side placed, or promises to not alter if something is declared as const (public parameter for example)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!