strdup.c

来自「一个c语言写做的编译器的源码」· C语言 代码 · 共 20 行

C
20
字号
/*@A (C) 1992 Allen I. Holub                                                */
#include <string.h>
#include <stdlib.h>
#include <tools/debug.h>	/* to map malloc() to dmalloc() if necessary */

char	*strdup( str )
char	*str;
{
    /* Save the indicated string in a malloc()ed section of static memory.
     * Return a pointer to the copy or NULL if malloc fails.
     */

    char *rptr;

    if( rptr = (char *) malloc( strlen(str) +1 ) )
	strcpy( rptr, str );

    return rptr;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?