LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-13-2005, 03:34 AM   #1
monil
LQ Newbie
 
Registered: Mar 2005
Posts: 19

Rep: Reputation: 0
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
 
Old 03-13-2005, 03:40 AM   #2
Hivemind
Member
 
Registered: Sep 2004
Posts: 273

Rep: Reputation: 30
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.
 
Old 03-13-2005, 03:56 AM   #3
monil
LQ Newbie
 
Registered: Mar 2005
Posts: 19

Original Poster
Rep: Reputation: 0
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
 
Old 03-13-2005, 10:23 AM   #4
live_dont_exist
Member
 
Registered: Aug 2004
Location: India
Distribution: Redhat 9.0,FC3,FC5,FC10
Posts: 257

Rep: Reputation: 30
you are not allocating memory to b .
Try char *b = (char *) malloc(sizeof(2000));
 
Old 03-13-2005, 10:26 AM   #5
Hivemind
Member
 
Registered: Sep 2004
Posts: 273

Rep: Reputation: 30
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.
 
Old 03-13-2005, 10:44 AM   #6
live_dont_exist
Member
 
Registered: Aug 2004
Location: India
Distribution: Redhat 9.0,FC3,FC5,FC10
Posts: 257

Rep: Reputation: 30
hey thnx 4 clearing my concepts ; but just a question ; what exactly does malloc(sizeof(2000))do if its not 2000 bytes..??
 
Old 03-13-2005, 10:51 AM   #7
aluser
Member
 
Registered: Mar 2004
Location: Massachusetts
Distribution: Debian
Posts: 557

Rep: Reputation: 43
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.
 
Old 03-13-2005, 12:28 PM   #8
exvor
Senior Member
 
Registered: Jul 2004
Location: Phoenix, Arizona
Distribution: Gentoo, LFS, Debian,Ubuntu
Posts: 1,537

Rep: Reputation: 87
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
 
Old 03-13-2005, 01:33 PM   #9
linuxzealot
Member
 
Registered: Feb 2005
Distribution: Gentoo
Posts: 57

Rep: Reputation: 15
Your first piece of code is fine, you just need to make sure you dont input more characters than your array can hold.
 
Old 03-13-2005, 02:20 PM   #10
aluser
Member
 
Registered: Mar 2004
Location: Massachusetts
Distribution: Debian
Posts: 557

Rep: Reputation: 43
linuxzealot: check that again, it doesn't initialize b : )
 
Old 03-13-2005, 02:40 PM   #11
linuxzealot
Member
 
Registered: Feb 2005
Distribution: Gentoo
Posts: 57

Rep: Reputation: 15
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.
 
Old 03-13-2005, 03:02 PM   #12
aluser
Member
 
Registered: Mar 2004
Location: Massachusetts
Distribution: Debian
Posts: 557

Rep: Reputation: 43
*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.
 
Old 03-13-2005, 03:08 PM   #13
linuxzealot
Member
 
Registered: Feb 2005
Distribution: Gentoo
Posts: 57

Rep: Reputation: 15
Oh ok, that makes sense , but I meant to put b as the pointer instead of putting the derefrencing sign * with it
 
Old 03-13-2005, 03:16 PM   #14
exvor
Senior Member
 
Registered: Jul 2004
Location: Phoenix, Arizona
Distribution: Gentoo, LFS, Debian,Ubuntu
Posts: 1,537

Rep: Reputation: 87
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.
 
Old 03-13-2005, 04:16 PM   #15
Dave Kelly
Member
 
Registered: Aug 2004
Location: Todd Mission Texas
Distribution: Linspire
Posts: 215

Rep: Reputation: 31
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
C# convert char array to string exodist Programming 3 09-16-2008 08:06 AM
array of char pointers djgerbavore Programming 2 01-08-2005 01:59 PM
search in char array xxfunkxx Programming 2 12-12-2004 11:23 PM
char array of size 10 can read morethan 10 chars!!!!! pippet Programming 13 07-12-2004 01:44 AM
Char array to int without losing value ? Dimitris Programming 3 01-14-2004 12:08 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:47 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration