简体   繁体   English

strcpy()实现..如何在函数本身中初始化指向函数的传递指针

[英]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. 我正在尝试实现strcpy()标准函数,该函数需要两个指向字符的指针并且什么也不返回。

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. 我已经正确完成了工作,但是为了避免分段错误,我必须在将其分配给strcpy()函数之前在主函数中初始化“目标”指针。 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. 我试图在strcpy()函数本身中进行此初始化,但是无法完成,因为我要通过值发送指针(字符的地址),因此尽管可以更改,但无法在函数中进行更改指针所指向的不是该点本身。

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. 将指针的指针传递给strcpy()函数可能是一种解决方案,但是我希望它的行为像标准函数一样,采用指向字符的指针而不是指向指针的指针。

Concisely, how can I avoid segmentation fault by initializing the pointer in the strcpy() function itself not in the main function? 简而言之,如何通过初始化strcpy()函数本身而不是main函数中的指针来避免分段错误?

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. 将指针的指针传递给strcpy()函数可能是一种解决方案,但是我希望它的行为像标准函数一样,采用指向字符的指针而不是指向指针的指针。

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'. 您也可以使用calloc函数代替malloc ,后者会自动将分配的内存初始化为'\\ 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. 仅对此做出响应: 我正在尝试实现strcpy()标准函数,该函数需要两个指向字符的指针并且不返回任何内容。

If that first sentence is an accurate representation of what you really want , 如果第一句话能准确表达您的真正意愿
Then write a wrapper around strcpy() : 然后围绕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. 这是简单的操作,但是通过一些错误检查,它将按照您在介绍句子中描述的方式进行操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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