Originally Posted by
Sebastiani
look at my original post. that's how you call the function.
I must be missing something. This just can't possibly be this difficult.
When I call the function as
Code:
good_function(p_source, p_dest);
it compiles fine, but the output is this:
The stings in thier origninal state:
This is the source string.
This is the destination string.
Now, we copy source into dest
Segmentation fault
Since I'm such a n00b, please let me post the whole business with this call in the main()?
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");
/*
void good_function(char *p_source, char *p_dest);
*/
good_function(p_source, 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;
}
}