Is it really worth it to use chars, especialy since I can strings?
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Is it really worth it to use chars, especialy since I can strings?
I'm getting tired of using chars, though, I know strings are just arrays of chars, it is just a lot easier for me pass arguements and copy one string to another, and do all other sorts of complex string manipulation that otherwise would have been too time consuming with chars.
From what I understand, c has libraries that mimic (or are. someone clarify?) c++ libraries. (such as stdlib.h, which [from what I understand] is cstdlib, or vice versa) So I may just be able to use a library (if one exists) that can deal with strings, in c, if I ever have to code in it.
My questsion is: Is it really needed that I use chars (I'm still going to learn more I can about it, along with c/c++ in general), or do you think that I could get by in many(if not most) programs with just string types. I realize that functions in SDL seem to need char*'s, but I know how to convert a string into a char, so that should not be a problem for me.
C++ has a string class. As C lacks support for classes, I'm not sure exactly what type of string you have in mind.
That being said, if working with c-strings (char arrays/pointers) is too troublesome for you, you might wish to explore just using C++ or some other higher-level language. Is there a particular reason for using straight C?
if you are using c++ you are better off to use std::string as much as possible. it is safer, and writes alot quicker. there is no reason that you have to use c strings, and if there was some reason that came up, like you said you know how to convert a std::string to a null terminated string so you would be ok.
As an aside note, the std::string class stores data internally as a cstring and internally uses much the same functions as you would use manipulating a cstring (strcpy, etc.). Its advantage revolves around the fact that buffer management is handled and it reduces the risk of off-by-one errors.
I'm not sure what your question is but in "c" you always use characters.
String is just an array of characters...
There are function such as strcpy, strcat, strlen which is just functions which is used to manuplate array of characters. You never need to do any sort of characters.
I'm not familiar with c++, so I can't comment on that.
From what I understand, c has libraries that mimic (or are. someone clarify?) c++ libraries. (such as stdlib.h, which [from what I understand] is cstdlib, or vice versa) So I may just be able to use a library (if one exists) that can deal with strings, in c, if I ever have to code in it.
sorry i overlooked this part earlier.. actually you have it backwards, the cstdlib is a c++ version of stdlib.h, which is just stdlib.h in the std namespace.. the same as cstring is string.h, ctime, cmath, etc..
you can make c++ use c style programming, but there is no way to make c use the functionality of c++.. when you introduce c++ to c, you no longer have c, you have c++.. but once again like i mentioned before, if c++ is an option, then by all means use all the things it has to offer.. including std::string and the STL.. it makes life much easier and safer..
Is it really worth it to use chars, especialy since I can strings?
Before the poor char get discarded to the wastebin of life, it still has a valid role when you only want a single character. Otherwise if you are using C++ then yes, by all means, go with the string class. However character arrays can be useful if you are doing very little in the way of manipulation and you wish to avoid the overhead of a class.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.