Applepine Applepine - 19 days ago 6
C Question

getting stuck in rewriting strcpy

Im trying to rewrite strcpy function but im getting stuck in this, my function does not run when the first arg is the static array of chars, only run if it is an allocated pointer. I think the problem is the realloc statement in my function. Please help me for my problem...

#include <stdio.h>
#include <stdlib.h>

int myStrlen(const char *);
char *myStrcpy(char *, const char *);

int main() {
char str1[40];
char str2[] = "one piece";
char *str;
str = (char*)malloc(1 * sizeof(char));
myStrcpy(str, str2);
printf("%s", str);
myStrcpy(str1, str2);
printf("%s", str1);
return 0;
}

int myStrlen(const char *str) {
int i = 0, len = 0;
while (str[i++] != '\0') {
len++;
}
return len;
}

char *myStrcpy(char *str1, const char *str2) {
int len1, len2;
len1 = myStrlen(str1);
len2 = myStrlen(str2);
str1 = (char*)realloc(str1, len2 * sizeof(char));
for (int i = 0; i < len2; i++) {
str1[i] = str2[i];
}
str1[len2] = '\0';
return str1;
}


Please help me to make my code using pointer work...
Btw, here is my function without using pointer, any ideas?

void myStrcpy(char str1[], char str2[]) {
int i = 0;
while (str2[i] != '\0') {
str1[i] = str2[i];
i++;
}
str1[i] = '\0';
}

void myStrncpy(char str1[], char str2[], int n) {
int i = 0;
while (str2[i] != '\0' || i <= n) {
str1[i] = str2[i];
i++;
}
str1[i] = '\0';
}

Answer

Your first implementation uses realloc() for no valid purpose. For standard function strcpy(), str1 is assumed to point to an array with enough space to store a copy of str2.

If your intent is to implement different semantics from that of strcpy, make the name more explicit and pass it the address of the destination pointer because realloc() is likely to return a different pointer. In your example, the original value of str is used after the call to myStrcpy(), which is incorrect.

Your second implementation is almost correct:

  • You should use size_t for the type of i. Strings can be longer than INT_MAX on current systems.

  • You should make the source strings const.

  • myStrncpy should stop when i reaches n, use && i < n

  • Testing i < n before dereferencing the str2 pointer allows an invalid pointer to be passed along with a zero value for n, which can be useful.

  • myStrcpy and myStrncpy should return the destination pointer.

  • Note that myStrncpy has much better semantics than strncpy. The standard function strncpy does not do what most programmers expect. Its semantics are error prone, it should not be used to avoid potential confusion.

Here is a simplified version:

#include <stdlib.h>

size_t myStrlen(const char *str) {
    size_t len = 0;
    while (str[len] != '\0') {
        len++;
    }
    return len;
}

char *myStrcpy(char *str1, const char *str2) {
    size_t i = 0;
    while (str2[i] != '\0') {
        str1[i] = str2[i];
        i++;
    }
    str1[i] = '\0';
    return str1;
}

char *myStrncpy(char *str1, const char *str2, size_t n) {
    size_t i = 0;
    while (i < n && str2[i] != '\0') {
        str1[i] = str2[i];
        i++;
    }
    str1[i] = '\0';
    return str1;
}