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.
I'm not familiar with rand() and I'm not on a machine where I can test what you've written (it looks needlessly complicated to me, but I could be missing something), but assuming the expression on the rhs of the sum operator generates a number between zero and 25, what you've got is a perfectly valid char and you may assign it as such (a char being a byte-wide value on my machine, anyway; 0-255).
Also, for readability you could change 97 to 'a'. Hard-coding sucks!
You'll want to change declaration for salt to char salt[3]; and after your for() loop add salt[2] = '\0';. Otherwise you'll get the occasional segfault.
It segfaults because it's not a real string unless it's NUL-terminated. All string functions know that the end of the string will be a NUL-terminator. If it's not present, then your string had 2 valid chars and then garbage after it. String functions would keep on scanning right through the garbage and cause all sorts of junk to happen
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.