problem in copying array to char *
Hi,
I am trying to copy array to char *, but get a "Segmentation fault (core dumped)" message Below is the code that i am using.. What wrong am i doing? Code:
-Monil |
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.
|
I tried the following...
Code:
#include <stdio.h> -Monil |
you are not allocating memory to b .
Try char *b = (char *) malloc(sizeof(2000)); |
Two things.
Dont cast what malloc returns. Code:
sizeof(2000) If you want allocate just enough space to store the string in the login array, allocate strlen(login) + 1 characters. |
hey thnx 4 clearing my concepts ; but just a question ; what exactly does malloc(sizeof(2000))do if its not 2000 bytes..??
|
sizeof(2000) is the same as sizeof(int), which is probably 4.
The version with strdup() looks fine to me, except perhaps that it would look best to free(b) when you're done with it. |
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 |
Your first piece of code is fine, you just need to make sure you dont input more characters than your array can hold.
|
linuxzealot: check that again, it doesn't initialize b : )
|
Well, what I was thinking was
*b is a pointer. And login of login[16] is a pointer to the first element in the array, and it is perfectly valid to do something such as b = login And I thought strcpy is basically doing the same thing, giving b the contents of login. :\ I could be wrong. |
*b isn't a pointer, but b is.
strcpy does this: Code:
char *strcpy(char *dest, const char *src) 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. |
Oh ok, that makes sense :D, but I meant to put b as the pointer instead of putting the derefrencing sign * with it :p
|
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. |
Quote:
Code:
#include <stdio.h> |
All times are GMT -5. The time now is 04:12 AM. |