sth about the string function strdup
2007-01-11 22:24
495 查看
As now I was using the fundtion strdup very often, so that I want to say sth about it, and I think that it just a function that combines the functions malloc and strcpy.
用法:#include <string.h>
功能:复制字符串s
说明:返回指向被复制的字符串的指针,所需空间由malloc()分配且可以由free()释放。
举例:
// strdup.c
#include <syslib.h>
#include <string.h>
main()
{
char *s="this is just f";
char *d;
d=strdup(s);
printf("%s",d);
getchar();
return 0;
}
strdup()主要是拷贝字符串s的一个副本,由函数返回值返回,这个副本有自己的内存空间,和s不相干。
char *strdup(const char *s)
{
char *t = NULL;
if (s && (t = (char*)malloc(strlen(s) + 1)))
strcpy(t, s);
return t;
}
用法:#include <string.h>
功能:复制字符串s
说明:返回指向被复制的字符串的指针,所需空间由malloc()分配且可以由free()释放。
举例:
// strdup.c
#include <syslib.h>
#include <string.h>
main()
{
char *s="this is just f";
char *d;
d=strdup(s);
printf("%s",d);
getchar();
return 0;
}
strdup()主要是拷贝字符串s的一个副本,由函数返回值返回,这个副本有自己的内存空间,和s不相干。
char *strdup(const char *s)
{
char *t = NULL;
if (s && (t = (char*)malloc(strlen(s) + 1)))
strcpy(t, s);
return t;
}
相关文章推荐
- Write a function to find the longest common common string amongst an array of strings.
- About the return value of SetTimer function
- SQL:Example Uses of the SUBSTRING String Function
- 高精度运算专题-输出函数与字符串转数字函数(Output function and the string to number function)
- Codeforces Round #429 (Div. 2/Div. 1) [ A/_. Generous Kefa ] [ B/_. Godsend ] [ C/A. Leha and Function ] [ D/B. Leha and another game about graph ] [ E/C. On the Bench ] [ _/D. Destiny ]
- FWNX- about the abs function -
- Write a function to find the longest common prefix string amongst an array of strings.
- some function about the GW ip address in the MS system
- About the String Functions of XSLT
- SQL:Example Uses of the SUBSTRING String Function
- Don't Forget the XPath string() function
- The Split Function -- use for std::string
- About the return value of the main function
- Write a function that takes a string as input and returns the string reversed.
- some functions about judge the String type
- about the character string in C language
- More about the Operations of string
- Given the string of parentheses only, write the function to check if they are balanced. ((())) is ba
- think about how to make the function close to really world
- ReactJs 报错 Element type is invalid: expected a string (from built-in components) or a class/function (for composite components) but got: undefined. Check the render method of `Me`.