C语言 strcpy 使用指针
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13460934/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
strcpy using pointers
提问by oridahan
I'm trying to write strcpy on my own using pointers and I get an error during runtime.
我正在尝试使用指针自己编写 strcpy 并且在运行时出现错误。
void str_cpy(char **destination, const char *source) {
// char *s1 = *destination;
while (*source != 'char *str = NULL;
str_cpy(&str, "String");
') {
**destination++ = *source++; //Get an error here
}
**destination = 'char *str = malloc(strlen("String") + 1); // + 1 for the 'char str[256]; // make str large enough to hold 256 chars. Note that this is not as safe as the above version!
' character at the end of C-style strings
';
}
I call the function as follows:
我调用函数如下:
void str_cpy(char **destination, const char *source) {
*destination = malloc(strlen(source) + 1);
// ... continue as normal
Is it not OK?
不好吗?
Thanks!
谢谢!
回答by Cornstalks
No, it's not okay. Why? Because stris a NULLpointer. It's pointing to nothing. When you try to write values into it, where will they go? It's not pointing to any allocated memory!
不,这不行。为什么?因为str是一个NULL指针。它没有指向任何东西。当您尝试将值写入其中时,它们会去哪里?它没有指向任何分配的内存!
You first have to allocate memory for str. You can do:
您首先必须为 分配内存str。你可以做:
void mystrcpy(char *dest, const char *src) {
while (*dest++ = *src++);
}
Or you can do:
或者你可以这样做:
void str_cpy(char *dst, const char *src) {
while (*src != 'const char *src = "String";
char *str = malloc(strlen(src)+1); //plus one for null byte
str_cpy(dst, src);
') {
*dst++ = *src++;
}
*dst = 'char *str = malloc(128);
if (str)
{
str_cpy(&str, "String");
free(str);
str = NULL;
}
';
}
Also, destinationshould be a single pointer, not a double pointer. Well, it's not technically wrong to use a double pointer, it's just unnecessary.
另外,destination应该是单指针,而不是双指针。好吧,使用双指针在技术上并没有错,只是没有必要。
Optionally, you can allocate the memory in the str_cpyfunction, like so:
或者,您可以在str_cpy函数中分配内存,如下所示:
char str[128];
str_cpy(&str, "Hello"); // error.
回答by squiguy
For simplicity's sake, this can be done in one line in a function.
为简单起见,这可以在函数的一行中完成。
char str[128];
char *p = str;
str_cpy(&p, "Hello"); //ok. passing address of pointer.
This being said, you do need to allocate memory for destbeforehand using mallocor just simply by having a character array like char dest[256].
话虽如此,您确实需要为dest预先使用分配内存,malloc或者只是简单地使用像char dest[256].
回答by iabdalkader
I don't see any need to pass a pointer-to-pointer:
我认为不需要传递指针到指针:
char* mystrcpy(char *dst, const char *src) {
char *ptr = dst;
while ((*dst++ = *src++) ) ;
return ptr;
}
int main(int argc, char *argv[]) {
const char *src = "This is C. void strcpy_i( char **dst, const char *src )
{
*dst=(char *)malloc((strlen(src)+1)*sizeof(char));
char *tmp=*dst;
if(tmp == NULL || src == NULL)
return ;
while((*tmp++=*src++)!='#include<stdio.h>
void main()
{
void mystrcpy(char *,char *);
char s1[100],s2[100];
char *p1;
char *p2;
p1=s1;
p2=s2;
printf("Enter the string to copy to s2...?\n");
scanf("%s",p1);
mystrcpy(p2,p1);
printf("S2 after copying = %s",p2);
}
void mystrcpy(char *p2,char *p1)
{
while(*p1!='##代码##')
{
*p2=*p1;
p2++;
p1++;
}
*p2='##代码##';
}
');
}
int main()
{
char v[]="Vinay Hunachyal";
char *d=NULL;
strcpy_i(&d,v);
printf("%s",d);
return 0;
";
char *dst = malloc(sizeof(char)*(strlen(src)+1)); //+1 for the null character
dst = mystrcpy(dst, src);
printf("%s",dst);
return 1;
}
And you need to allocate memory for dstbefore passing:
并且您需要dst在传递之前分配内存:
回答by WhozCraig
You should likely allocate some memory for that pointer before passing it off to a function that fills what it points to(which in this case, is NULL).
在将它传递给填充它指向的内容(在这种情况下为 NULL)的函数之前,您可能应该为该指针分配一些内存。
Example:
例子:
##代码##I advise not doing this without also providing target-buffer size information (i.e. if you're writing your own, then boundary-check the target buffer, otherwise your version has the same security flaws as strcpy()which are bad enough as it is).
我建议不要在不提供目标缓冲区大小信息的情况下执行此操作(即,如果您正在编写自己的缓冲区,则对目标缓冲区进行边界检查,否则您的版本具有相同的安全漏洞,因为strcpy()它已经足够糟糕了)。
Note: Unless you're planning on changing the address held by the pointer passed as the target, you need not use a double pointer either. The double pointer usage you have prevents the traditional strcpy()usage pattern of:
注意:除非您计划更改作为目标传递的指针所持有的地址,否则您也不需要使用双指针。您拥有的双指针用法可以防止以下传统strcpy()使用模式:
An array address cannot be passed as a pointer-to-pointer, so your code cannot fill a static array without an intermediate pointer:
数组地址不能作为指针传递,因此您的代码无法在没有中间指针的情况下填充静态数组:
##代码##If this is not intentional (and I don't see why it could be unless you have ideas of internally emulating strdup()on a NULL pointer passage) You should address this.
如果这不是故意的(我不明白为什么会这样,除非您有内部模拟strdup()NULL 指针通道的想法)您应该解决这个问题。
回答by Bibhu Mohapatra
Here is a complete implementation. Good article from here. Describes timing and performance. I did not measure myself though. http://www.howstuffworks.com/c35.htm
这是一个完整的实现。好文章来自这里。描述时间和性能。虽然我没有测量自己。 http://www.howstuffworks.com/c35.htm
##代码##回答by vinay hunachyal
Recently I faced same problem of above one using double pointer strcpy implementation
最近我遇到了与上述相同的问题 double pointer strcpy implementation
It might helpful to others below code
它可能对以下代码的其他人有帮助
##代码##}
}
回答by vinay hunachyal
Its my solution..Simple to understand..
这是我的解决方案..简单易懂..

