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.
Well there are a few things to do, and note what you wrote is C not C++. (Subtle difference, ) To maintain compatibility with your main() you'll want to not make newpw a pointer, rather an array... like this
The problem was that char *newpw is just a pointer to a char, it does not automatically allocate space. if you wanted to do that you'd have to do this.
And this program suffers from a buffer overflow too in main() from char entered[80]. What if I enter a password that is 1024 characters long? Think of a solution for that. Also note that a bash script that uses mkpasswd would probably be easier to write, more effient, and more secure.
you should allocate 9 chars if you're going to use an index like [8]. If you only allocate 8, it may segfault for some compilers, or overwrite stuff you don't want to overwrite etc.
portability. There are more C compilers out there than c++ compilers. Once upon a time the gnu c++ compiler generated slower code, even when invoked on valid C -- I don't know if that's still true. Still, if you are going to require a c++ compiler you might was well make use of the stl <string> class or at least switch to operator "new" and "delete" instead of malloc/calloc/free. If your code gets linked to other peoples' projects, it's doubly important for it to not be schizophenic about it's c or c++ness, as they link differently.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.