LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 04-12-2005, 06:46 PM   #1
alltime
Member
 
Registered: Jan 2005
Distribution: Mandrake 10.1 with kernel 2.6.10
Posts: 60

Rep: Reputation: 15
Problem with copying string


Within a program that I am writing, I have a feature where the users enters an input and then the program copies the input to another char. Problem is that I receive the following error upon compiliation: assignment makes integer from pointer without a cast.

Here is a snippet of the code that I have written:

int main(int argc, char* argv[])
{
char line[256];
char linecopied;
char cmd[sizeof(line)], *args;
int i;
int linelen;

/* Get user input */
if(fgets(line, sizeof(line), stdin) == NULL) continue;

printf("lines is %s\n", line);
linecopied = line;

/* Return the lenght of the line */
linelen = strlen(line);


...
 
Old 04-12-2005, 07:21 PM   #2
trevilli
LQ Newbie
 
Registered: Mar 2004
Location: San Francisco Bay Area
Distribution: W: CentOS H: Ubuntu
Posts: 10

Rep: Reputation: 0
RE: Problem with copying a string

Here's one problem that I saw. You are making an assignment from a pointer into a character. My editor calls it line 16, but here is the problem statement:

linecopied = line;

line is the name of the array. The name line is actually a pointer to the first character in the array, so your statment tries to stuff a pointer type into a character type, hence the error. If you want the first character in the array line, try something like this:

linecopied = line[0];

Now you are making an assignment from a character to a character. It's not clear to me from your code exactly what you are trying to do, but I hope this clears up your error and your confusion
 
Old 04-12-2005, 08:28 PM   #3
alltime
Member
 
Registered: Jan 2005
Distribution: Mandrake 10.1 with kernel 2.6.10
Posts: 60

Original Poster
Rep: Reputation: 15
Oh I understand.

How could I copy the entire string in "line" to "linecopy" instead of just the first character?
 
Old 04-12-2005, 08:38 PM   #4
trevilli
LQ Newbie
 
Registered: Mar 2004
Location: San Francisco Bay Area
Distribution: W: CentOS H: Ubuntu
Posts: 10

Rep: Reputation: 0
You need to use the strcpy function. Does your array have more then one string in it? If so you need to delimit the strings by a marker, such as a null character. For this I would suggest looking into the strtok function, which breaks up a string into a series of tokens. I think there are man pages for these, as they are standard C functions, and they should get you going in the right direction.
 
Old 04-12-2005, 09:22 PM   #5
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 373Reputation: 373Reputation: 373Reputation: 373
Another thing: you misunderstand what the sizeof() operator does. It does not return the length of a string in characters. It returns the number of bytes required for the data type. A "char *" only occupies 4 bytes of memory regardless of how many characters are in the string.

If you want to know how many characters are in the string, you need to use strlen(). Keep in mind, you cannot use strlen() when defining the size of a data type, as in:
Code:
 char another_string[strlen(first_string)];
That's illegal, and will cause an error. To do that sort of operation, you need to do memory allocation on your own. Normally, that's done with malloc() and free(). As an example:
Code:
string_length  = strlen( first_string ) + 1;              // Equals length of first_string + the NULL
another_string = malloc( sizeof(char) * string_length );  // Reserve space for a copy of first_string
memset( another_string, 0, string_length );               // Initialize the memory to all 0's
strcpy( another_string, first_string );                   // Copies first_string into another_string

... // Do other fancy stuff

free( another_string );                                   // Release the memory used by another_string
 
Old 04-13-2005, 09:05 PM   #6
alltime
Member
 
Registered: Jan 2005
Distribution: Mandrake 10.1 with kernel 2.6.10
Posts: 60

Original Poster
Rep: Reputation: 15
.

Last edited by alltime; 04-13-2005 at 09:52 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
problem copying a dvd d1l2w3 Linux - Software 18 09-09-2005 09:43 PM
proftp copying problem zatka Linux - Networking 2 01-11-2005 10:27 PM
Problem in copying a file in C akin81 Programming 11 04-06-2004 08:06 AM
java test if string in string array is null. exodist Programming 3 02-21-2004 01:39 PM
String copy problem rajatgarg Programming 1 11-14-2003 06:48 PM

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

All times are GMT -5. The time now is 06:14 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration