0

I'm working on a C assignment that's basically making our own C String class. My partner and I are confident that we have the logic correct and our source files are compiling, but we're unable to get any output on a simple main file.

Our strcpy is as follows:

...
char *my_strcpy(char *s1, const char *s2)
{
    int r=0;    
    for (int i=0;s2[i]!='\0'; i++)
    {
        s1[i]=s2[i];
        r++;
    }
    s1[r+1]='\0';
    return s1;  
}
...

as for our main file we have:

...
const char *hello = "Hello World! ";
char pointer[1024];

my_strcpy(pointer, hello);
printf("%s\n", pointer);
...

any help would be greatly appreciated! thanks!

4 Answers4

2

Revised code

char *my_strcpy(char *s1, const char *s2)
{
    int r=0;    
    for (int i=0;s2[i]!='\0'; i++)
    {
        s1[i]=s2[i];
        r++;
    }
    s1[r+1]='\0';
    return s1;  
}

Revised answer

You don't need both r and i. You have an off-by-one error at s1[r+1] = '\0';.

Working code

Correct for both original and revised versions of the question.

char *my_strcpy(char *s1, const char *s2)
{
    int i;    
    for (i = 0; s2[i] != '\0'; i++)
        s1[i] = s2[i];
    s1[i] = '\0';
    return s1; 
}

Original code

char *my_strcpy(char *s1, const char *s2)
{
    int r=0;    
    for (int i=0;*s2[i]!='\0'; i++)
    {
        *s1[i]=*s2[i];
        r++;
    }
    *s1[r+1]='\0';
    return *s1; 
}

Original answer

The code should not be compiling without lots of warnings (actually, they should be errors).

Throughout the code, the notation:

*s1[i]

is dereferencing a character as if it were a character pointer.

You're also 'off-by-one' in *s[r+1] = '\0';

You're returning a char as a char *.

You don't really need both i and r.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
2

There are many wrong on your codes:

Compile Error:

  1. my_strcpy need returning a pointer of char: char*, but you return a char.

Logic Error:

  1. s2[i]!='\0' is right

So your code may look like this:

char *my_strcpy(char *s1, const char *s2)
{
    int r=0;    
    for (int i=0;s2[i]!='\0'; i++)
    {
        s1[i]=s2[i];
        r++;
    }
    s1[r]='\0';
    return s1; 
}

I think you are not familiar with the pointer in C.

QJGui
  • 907
  • 8
  • 10
1

You are returning *s1, it should be s1 since you need to return a pointer.

Eric Fortin
  • 7,533
  • 2
  • 25
  • 33
0

First, a coding error, you are defining int i = 0 in your for loop. All variables should be defined before you do anything else in the function.

Second, you want to copy over your last character which is the null at 'r'. Your loop increments r until you find a null character so why would you want to place the null character one place after where you find it? Change it to s1[r]='\0';

One thing that I have noticed with your my_strcpy function is that you are returning s1 but when you are calling my_strcpy, you are not assigning the return value to a pointer. This is fine because you first argument is an "output" because you are passing in a pointer. I would make the my_strcpy return void and get rid of the return s1; line all together.

All together, the my_strcpy would look like this

void my_strcpy(char *s1, const char *s2)
{
    int r = 0;
    int i = 0;
    for (i = 0; s2[i] != '\0'; i++)
    {
        s1[i] = s2[i];
        r++;
    }
    s1[r] = '\0';
}
  • Welcome to Stack Overflow. Please read the [About] page soon. Note that C11 and C99 permit you define variables in the first clause of a `for` loop, though C89 did not (and MSVC still does not because it is a C89 compiler). In your revised code, when does `r` not equal `i`? Given that, why do you have both? – Jonathan Leffler Mar 05 '14 at 04:07