function to copy one string to another one

This is a discussion on function to copy one string to another one within the C Programming forums, part of the General Programming Boards category; Hello, all.
I am working through problems in the book Sam's Teach Yourself C for Linux in 21 Days and ...

function to copy one string to another one

Hello, all.
I am working through problems in the book Sam's Teach Yourself C for Linux in 21 Days and all of a sudden I have hit a brick wall.

The exercise I am trying now is to write a function that will copy one string into another string. I am able to do this inside of main(), but the exercise is to do it in a function outside of main(). I am new, and believe that usually strcpy() is used to do this sort of thing, but am feeling defeated by this, and have been trying for three days.

I have read the stickies and hope that my posting the entire (short) program is not too far out of bounds, since I am so new that there could be errors anywhere.

This *should* be fairly straight forward. There was an example of such a thing online from O'Reilly's Practical C but, I couldn't get it to work outside of main.

to Sebastiani:
Regarding passing the pointers to the function instead of the name,
I think this author was trying to use this for demonstration purposes,
In that this book is for people like me who are scrambling to learn
the very fundamentals. That being said, I am under the impression
that while that function call is not the best way to do it, that it
should work, nonetheless, though unnecessarily complicated?

Yes! You wrote: "both are pointers to the data - either will work fine."
Thank you for clearing that up! And, along with my apologies for the
line numbers, as I'm off to experiment with this, here is the code again,
this time sans numbers (in case anyone else wants to mess with this).
I'm still perplexed about the above error regarding
error: parse error before "char"

But, I will *NOT* give up on this! NO!

Code:

/* desperate.c */
/* this is a fresh start at trying to do exercise9_5 in the */
/* 21 Days book. */
/* Write a function that copies one array of characters */
/* into another. (Hint: Do this just as in the programs you */
/* wrote on Day 8). */
#include<stdio.h>
char source[32] = "This is the source string. ";
char dest[32] = "This is the destination string.";
/*12345678901234567890123456789012*/
/* there are 32 slots in each array */
char *p_source = &source[0];
char *p_dest = &dest[0];
void good_function(char *p_source, char *p_dest);
int main(void)
{
puts("The stings in thier origninal state: \n");
puts(source);
puts(dest);
puts("\nNow, we copy source into dest");
good_function(char *p_source, char *p_dest);
puts("here they are after that is done: \n");
puts(source);
puts(dest);
return 0;
}
void good_function(char *p_source, char *p_dest)
{
while (1)
{
*p_dest = *p_source;
/* exit if we copied the end of the string */
if (*p_dest == '\0')
break;
++p_dest;
++p_dest;
}
}

Well, thank you so much, because now I have a working example of how it is possible to do this. (I don't care right now if its the best way, only if it will work at all).

Here is what I have for it now, thanks to you guys. Interestingly, if I try to not use the keyword void in front of the call to the function, the gcc compiler complains. Maybe it is something particular to that compiler, AFAIK.
Thank you again!

Instead of using a while(1) you could instead be using the source string as the loop control. Using a do-while, you could have it terminate when it reaches the end of the source string, after having copied the null over. I'll leave that rewrite as an exercise to the reader.

Doh!
A gross oversite! Yes, it works famously now!
Thank you all so much! It was something, hitting that wall the last three days. Up till then, it was going much smoother. But, its all good.

To Quzah:
Yes, I am going to work through this book first, to get a rough overview, and
then, on the next go 'round, I will work through the O'Reilly book, Practical C , and by then I will be accustomed to at least the basic ideas, so that I can do things in a more efficient way, or what have you. As for now, I'm still struggling to get the stuff to work.

Practical C quite often does not get good reviews, so keep that in mind when you go though it. You can take a look here. You could also search this forum for that title - I am sure some comments have been made about it. Incidentally, ACCU has a fairly decent book review section to look over. It is a helpful reference when you are trying to decide on a book to buy. Your Sams book is not bad, though there are some inaccuracies in it as well. Lurk on this board and you will learn about the things you should and should not do when writing a C program.

Practical C quite often does not get good reviews, so keep that in mind when you go though it. You can take a look here. You could also search this forum for that title - I am sure some comments have been made about it. Incidentally, ACCU has a fairly decent book review section to look over. It is a helpful reference when you are trying to decide on a book to buy. Your Sams book is not bad, though there are some inaccuracies in it as well. Lurk on this board and you will learn about the things you should and should not do when writing a C program.

~/

Thank you, kermit.
I already have the Practical C, and am now SURE that reading over these posts here will be a must-do for me!