Reputation: 23
My task is like this: I should implement the strcpy function under the following constraints:
*(d+i)
)<string.h>
I'm programming in Visual Studio 2019. I searched some source code in google and run them, but my program has a logical error. The program ends right away, each time. I don't know what I'm doing wrong. Here's my code in Visual Studio 2019 on Windows. Please tell me what's wrong.
#include <stdio.h>
void strcpy(char*, char*);
int main()
{
char* sen1 = "Hello";
char* sen2 = "Friends";
strcpy(sen1, sen2);
printf("The result: %s\n", sen1);
return 0;
}
void strcpy(char* str1, char* str2)
{
int i = 0;
while (*(str2 + i) != '\0')
{
*(str1 + i) = *(str2 + i);
i++;
}
*(str1 + i) = '\0';
}
Upvotes: 1
Views: 1583
Reputation: 84521
In addition to needing to provide writable storage for sen1
, you should also check to ensure str2 != NULL
in your function before dereferencing str2
(otherwise, even if you fix all other errors -- a segfault will likely result)
For example, in your code you can define a constant to use in setting the size of a sen1
array (or you can allocate storage with malloc()
, calloc()
, or realloc()
-- save that for later). Using an array you can do, e.g.
#include <stdio.h>
#include <stdlib.h>
#define MAXC 64 /* if you need a constant, #define one (or more) */
...
int main (void)
{
char sen1[MAXC] = "Hello";
char *sen2 = "Friends";
mystrcpy (sen1, sen2);
printf ("The result: %s\n", sen1);
}
In your strcpy function, check that str2
isn't NULL
before using str2
in your function, e.g.
char *mystrcpy (char *dest, const char *src)
{
char *p = dest;
if (!src || !dest) { /* ensure src or dest is not NULL */
fputs ("error: src or dest parameters NULL in mystrcpy().\n", stderr);
exit (EXIT_FAILURE);
}
do /* loop */
*p++ = *src; /* copy each char in src to dest */
while (*src++); /* (including the nul-termianting char) */
return dest; /* return pointer to dest */
}
Now you will copy your source string to your destination string in your (renamed) mystrcpy()
function, receiving the results you expect:
Example Use/Output
$ ./bin/mystrcpy
The result: Friends
Look things over and let me know if you have further questions.
Upvotes: 5
Reputation: 15032
Among the other good answers, just regarding the implementation of your strcpy
function and not a detailed issue analyze of your actual code, another approach is this:
char * n_strcpy(char * dest, char const * src)
{
if (dest == NULL || src == NULL)
{
return NULL;
}
char *ptr = dest;
while ((*dest++ = *src++));
return ptr;
}
Upvotes: 0
Reputation: 72619
Two problems, at least:
strcpy
for your own function. Use another name.Three clean code issues, at least:
int main()
into int main(void)
to make it properly typed.str1
and str2
are too generic names. They don't indicate which is the source and which is the destination pointer. What about my_strcpy(char *dest, char *src)
?size_t i
for the index counter instead of int
, because that's the type all the string length functions and the sizeof
operator return. It's also an unsigned type and can copy really long strings :-) The size_t
is available after #include <stddef.h>
.Upvotes: 3
Reputation: 50778
You want this:
...
char* source = "Hello";
// or char source[] = "Hello";
char destination[1000]; // destination buffer long enough for playing around
my_strcpy(destination, source);
printf("%s\n", destination); // should print "Hello" if my_strcpy is corect
...
For the rest read Jens's answer.
Upvotes: 2