LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   problem in copying array to char * (https://www.linuxquestions.org/questions/programming-9/problem-in-copying-array-to-char-%2A-301016/)

monil 03-13-2005 03:34 AM

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:


#include <stdio.h>
#include <string.h>

int main()
{
        char *b;
        char login[16];

        printf("Please enter some text: ");
        scanf("%s", login);

        strcpy(b,login);

        printf("%s", b);
        return 0;
}

Thank you.

-Monil

Hivemind 03-13-2005 03:40 AM

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.

monil 03-13-2005 03:56 AM

I tried the following...

Code:

#include <stdio.h>
#include <string.h>

int main()
{
        char *b;
        char login[16];

        printf("Please enter some text: ");
        scanf("%s", login);


        b = strdup(login);
//      strcpy(b,login);

        printf("%s", b);
        return 0;
}

Is it what you what you are telling?

-Monil

live_dont_exist 03-13-2005 10:23 AM

you are not allocating memory to b .
Try char *b = (char *) malloc(sizeof(2000));

Hivemind 03-13-2005 10:26 AM

Two things.
Dont cast what malloc returns.
Code:

sizeof(2000)
won't give you 2000 bytes if that's what you think.

If you want allocate just enough space to store the string in the login array, allocate strlen(login) + 1 characters.

live_dont_exist 03-13-2005 10:44 AM

hey thnx 4 clearing my concepts ; but just a question ; what exactly does malloc(sizeof(2000))do if its not 2000 bytes..??

aluser 03-13-2005 10:51 AM

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.

exvor 03-13-2005 12:28 PM

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

linuxzealot 03-13-2005 01:33 PM

Your first piece of code is fine, you just need to make sure you dont input more characters than your array can hold.

aluser 03-13-2005 02:20 PM

linuxzealot: check that again, it doesn't initialize b : )

linuxzealot 03-13-2005 02:40 PM

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.

aluser 03-13-2005 03:02 PM

*b isn't a pointer, but b is.

strcpy does this:
Code:

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.

linuxzealot 03-13-2005 03:08 PM

Oh ok, that makes sense :D, but I meant to put b as the pointer instead of putting the derefrencing sign * with it :p

exvor 03-13-2005 03:16 PM

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.

Dave Kelly 03-13-2005 04:16 PM

Quote:

Originally posted by live_dont_exist
you are not allocating memory to b .
Try char *b = (char *) malloc(sizeof(2000));

I
Code:

#include <stdio.h>
#include <string.h>

int main()
{
        char *b;
        char login[16];

        printf("Please enter some text: ");
        scanf("%s", login);
        printf("test 1\n");

        strcpy(b,login);
        printf("test 2  %s %s\n", login, b);

        printf("%s\n", b);
        printf("test 3\n");

        return 0;
}

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.


All times are GMT -5. The time now is 04:12 AM.