简体   繁体   中英

strcpy() implementation.. How to initialize a passing pointer to a function in the function itself

I'm trying to implement strcpy() standard function, as a function taking two pointers to characters and returning nothing.

I've done the job right, but to avoid segmentation fault I had to initialize "dest" pointer in the main function before assigning it to the strcpy() function. I've tried to do this initialization in the strcpy() function itself but couldn't because I'm sending the pointer (the address of the characters) by value, so I can't change it in the function although I can change what the pointer points to not the point itself.

May be a solution to pass a pointer to pointer to the strcpy() function, but I want it to act like the standard function, taking pointers to character not a pointer to pointer.

Concisely, how can I avoid segmentation fault by initializing the pointer in the strcpy() function itself not in the main function?

I hope my question is clear

Here is my code :

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

void myStrcpy(char *dest,const char *src){

    while(*src)
    {
        *dest = *src;
        dest++;
        src++;
    }
    *dest='\0';
}

int main(void){

    char *src="Salahuddin";
    int len=strlen(src);
    char *dest=(char*)malloc(len);


    myStrcpy(dest,src);


    printf("%s",dest);

    return 0;
}

Indirection only goes one-way. You cannot change the pointer unless you pass its address in or return a value to it.

您不能不更改API的签名。

You can declare your function as

void myStrcpy(char *&dest,const char *src)
                    ^   look here!

It will solve this problem

May be a solution to pass a pointer to pointer to the strcpy() function, but I want it to act like the standard function, taking pointers to character not a pointer to pointer.

Why couldn't you do this?

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

void myStrcpy(char *dest, const char *src)
{
    if (dest == NULL) {
        dest = (char *) malloc(strlen(src));
    }

    while(*src) {
        *dest = *src;
        dest++;
        src++;
    }
    *dest='\0';
}

int main(void)
{
    char *src="Salahuddin";
    int len=strlen(src);
    char *dest=NULL;

    myStrcpy(dest, src);

    printf("%s", dest);

    return 0;
}

You could also use the calloc function instead of malloc , which automatically initializes the allocated memory to '\\0'.

Of course, you have to be careful and make sure you free your memory in either scenario... but you should be able to allocate the memory in the function.

Responding to this only: I'm trying to implement strcpy() standard function, as a function taking two pointers to characters and returning nothing.

If that first sentence is an accurate representation of what you really want ,
Then write a wrapper around strcpy() :

int main(void)
{
    char *dest;
    char src[] = {"some string");
    dest = calloc(sizeof(src), sizeof(char));
    myStrcpy(dest, src);
    //use dest
    free(dest);
    return 0;
}

void myStrcpy(char *dest, char *src)
{
    strcpy(dest, src);
}  

This is bare bones, but with some error checking, it will do what you described in your intro sentence.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM