Strcpy in C with pointers

2

I'm creating a function in C that copies the string s2 in s1 but it does not work and I do not know where my fault is.

My code is as follows:

char* mi_strcpy(char* s1, char* s2){
    char* puntero = (char*) malloc (mi_strlen(s2)*sizeof(char));
    while(*s2!='
char* mi_strcpy(char* s1, char* s2){
    char* puntero = (char*) malloc (mi_strlen(s2)*sizeof(char));
    while(*s2!='%pre%'){
        *puntero = *s2;
    puntero++;
    s2++;
    }

    *puntero='%pre%';
    s1 = puntero;
    return s1;
}
void test_mi_strcpy()
{
    char* s1 = "abcdefgh";
    char s2 [9];
    INICIO_TEST("mi_strcpy");
    DebeSerCierto(mi_strcpy(s2, s1) == "abcdefgh");
    FIN_TEST("mi_strcpy");
}
'){ *puntero = *s2; puntero++; s2++; } *puntero='%pre%'; s1 = puntero; return s1; } void test_mi_strcpy() { char* s1 = "abcdefgh"; char s2 [9]; INICIO_TEST("mi_strcpy"); DebeSerCierto(mi_strcpy(s2, s1) == "abcdefgh"); FIN_TEST("mi_strcpy"); }
    
asked by Juanma 23.02.2017 в 09:49
source

3 answers

3
char* mi_strcpy(char* s1, char* s2){
    char* puntero = (char*) malloc (mi_strlen(s2)*sizeof(char));
    while(*s2!='
void func(int a)
{ a = 5; }

int main()
{
  int var = 0;
  func(var);
  printf("%d",var);
}
'){ *puntero = *s2; puntero++; s2++; } *puntero='
char* mi_strcpy(char* s1, char* s2){
    char* puntero = s1;
    while(*s2!='
char* mi_strcpy(char* s1, const char* s2){
  char *puntero = s1;
  do
  {
    *puntero++=*s2;
  } while(*s2++);

  return s1;
}
'){ *puntero = *s2; puntero++; s2++; } *puntero ='
char* mi_strcpy(char* s1, char* s2){
    char* puntero = (char*) malloc (mi_strlen(s2)*sizeof(char));
    while(*s2!='
void func(int a)
{ a = 5; }

int main()
{
  int var = 0;
  func(var);
  printf("%d",var);
}
'){ *puntero = *s2; puntero++; s2++; } *puntero='
char* mi_strcpy(char* s1, char* s2){
    char* puntero = s1;
    while(*s2!='
char* mi_strcpy(char* s1, const char* s2){
  char *puntero = s1;
  do
  {
    *puntero++=*s2;
  } while(*s2++);

  return s1;
}
'){ *puntero = *s2; puntero++; s2++; } *puntero ='%pre%'; return s1; }
'; s1 = puntero; return s1; }
'; return s1; }
'; s1 = puntero; return s1; }

Where are you using s1 ? You make an assignment at the end, but do not forget that the pointer s1 , as such, is a local variable of your function. At the beginning it's a bit complex to understand, but look at the following example:

%pre%

It is clear that the program will print 0 because the change made by func is local. With pointers the exact same thing happens, the pointer is a local variable and the only thing that is shared with the outside is what is stored in the pointed memory address. If you make the pointer point to another site you are only making a local change because the original memory does not change .

In your case you should create a temporary pointer based on s1 and work with this pointer:

%pre%

Another alternative:

%pre%     
answered by 23.02.2017 в 10:04
1

If you want to copy one string to another, and you are giving the two strings as parameters, what you can do is modify the first string because it is also a pointer. Try this:

void mystrcpy(char * s1, char * s2){
    while(*s2!='
void mystrcpy(char * s1, char * s2){
    while(*s2!='
void mystrcpy(char * s1, char * s2){
    while(*s2!='
void mystrcpy(char * s1, char * s2){
    while(*s2!='%pre%'){
        *s1 = *s2;
    s1++;
    s2++;
    }
    *s1='%pre%';
}
'){ *s1 = *s2; s1++; s2++; } }
'){ *s1 = *s2; s1++; s2++; } *s1='%pre%'; }
'){ *s1 = *s2; s1++; s2++; } }

You should order the creation of the pointer with its size before calling this method.

EDIT:

When the chain is finished copying, the null character is added to the end, to complete it. Staying this way:

%pre%     
answered by 23.02.2017 в 16:24
0

I would have allocated less memory from chain 1 to 2 haha

malloc(strlen(s1)+strlen(s2)+2)

This could be your own strcpy:

char *mi_strcpy(char *cadena1, char *cadena2) {
    char *cadena = malloc(strlen(cadena1)+strlen(cadena2)+2);
    sprintf(cadena, "%c", 0);
    for (int i=0; i<strlen(cadena1); i++) {
        sprintf(cadena, "%s%c", cadena, cadena1[i]);
    }
    for (int i=0; i<strlen(cadena2); i++) {
        sprintf(cadena, "%s%c", cadena, cadena2[i]);
    }
    return cadena;
}

Usage:

int main() {
    char *mi_copia = mi_strcpy("hola ", "mundo");
    printf("%s\n", mi_copia);
    free(mi_copia);
}
    
answered by 09.03.2017 в 11:29