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.
b doesn't point to a valid block of memory. You must make sure it does that and that that memory block is big enough to hold the string you're trying to copy.
One thing to keep in mind with scanf if you declare the string/array to be 12 then if the user inputs more information this will not be copied or may cause issues with your program. rember that 12 in a string is actually only 11 real charecters because the terminating char is one of them.
in any account because your orignal string is onlly 12 char in size you could have just declared b[12] and it would be simpler.
then again im not a very seasoned c programmer and my logic maybe flawed twised or just plain wrong :P
char *strcpy(char *dest, const char *src)
{
int i = 0;
while (src[i] != '\0') {
dest[i] = src[i];
++i;
}
dest[i] = '\0';
return dest;
}
and no, that's not the real source code; I wrote it without testing
so anyway, you'd be writing to a random place in memory. if strcpy were really just an assignment, A) it wouldn't be useful since you could use = instead and B) it'd have to have a different prototype, since it can't change the address in b; variables are passed by value in C.
The reason strcpy exists is so that you can get a copy of a string to change, without changing the original. if you do b = login; and manipulate the string pointed to by b, login changes as well.
I'm a little confused on when to initialize an array and when not to. i understand the reason why is to set all the elements to a 0 is wise cause then your data in the array is not going to contain garbage.
strcpy is exactly as was pointed out and thanks for that cause i was wondering the usefulness for it as well.
The only way I could get a segmentation fault was to input a word of length 16 charecters or larger. And testing does seem to indicate that occurs in the attempt to copy to the pointer b.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.