Jump to content
  • Advertisement
Sign in to follow this  
TheNobleOne

char ** or string

This topic is 4848 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

Ok I am trying to further my C++ knowledge. So what does "char **" actually mean? Also, is a string better practice to use then this?

Share this post


Link to post
Share on other sites
Advertisement
char ** means a pointer to a pointer to a char. This can mean a pointer to an array of null terminated strings, or it could mean the address to which to place a character array. Without commenting, knowing what a given char ** is going to be used as can be tricky.

And string objects are your friend.

Share this post


Link to post
Share on other sites
A "char**" is a pointer to a pointer to a char: it's the address of a variable that contains the address of a "char" value. This means it can be:

- A pointer to a pointer to a char
- An array of pointers to a char
- A pointer to an array of char
- An array of arrays of char

And concerning your char*/string question: std::string does the same things as char*, but in a safer way and also does not require a deep knowledge of pointers to use. It also provides a function that transforms it into a char* if you need to pass it to a C function. So my advice is to use string over char*.

Share this post


Link to post
Share on other sites
A char** is a pointer to a pointer to one (or more) character(s).

std::string takes care of a lot of memory manipulation for you, and is usually safer, but might not be safe to pass across a DLL boundary (depending on your OS and environment).

Share this post


Link to post
Share on other sites
Ok thank you. I know C++ well as I have been learning it for about 3 1/2 years now there are a few things i am still shady on. Now in SDL there is a SetCaption fuction. SDL_WM_SetCaption(char **caption, char **icon); So I can pass a string object to the caption argument or will I have to convert it first?

Share this post


Link to post
Share on other sites
The function signature is


void SDL_WM_SetCaption(const char *title, const char *icon);




You can obtain a const char* from a std::string instance by invoking its c_str() member.





std::string title = "mytitle";
std::string icon = "myicon";

SDL_WM_SetCaption(title.c_str(), icon.c_str());



Share this post


Link to post
Share on other sites
Quote:
Original post by TheNobleOne
So I can pass a string object to the caption argument or will I have to convert it first?


You do this by using c_str() method of the std::string.

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!