简体   繁体   English

如何使用 strcpy 作为指针?

[英]How to use strcpy for pointers?

Why it stops working?为什么它停止工作? Can't we pass the name of the pointer as an argument for strcpy?我们不能将指针的名称作为 strcpy 的参数传递吗? if I change it to strcpy(&a,&b);如果我把它改成strcpy(&a,&b); it works.有用。

#include <stdio.h>

    int main() {

    char *a;
    a = "aabtyn";

    char *b;
    b =  "mihli";

    strcpy(a,b);

    printf("%s-%s\n",a,b);


    return 0;
}

Can't we pass the name of the pointer as an argument for strcpy ?我们不能将指针的名称作为参数传递给strcpy吗?

Yes, we can.我们可以。 However, it is also important that the destination pointer points to a writeable memory;但是,目标指针指向可写内存也很重要; in your case, neither a nor b point to memory that can be written.在您的情况下, ab都没有指向可以写入的内存。

if I change it to strcpy(&a,&b);如果我把它改成strcpy(&a,&b); it works.有用。

If it appears to work on your system, it does so by mistake.如果它看起来可以在您的系统上运行,那么它是错误的。 What you see is undefined behavior.你看到的是未定义的行为。 In order for strcpy to work you need to do one of the following:为了使strcpy工作,您需要执行以下操作之一:

  • Allocate a in automatic memory by defining it as char a[] = "aabtyn";通过将a定义为char a[] = "aabtyn";在自动内存中分配a char a[] = "aabtyn"; , or , 或者
  • Allocate a in dynamic memory by calling char *a = malloc(7);通过调用char *a = malloc(7);在动态内存中分配a You need seventh byte for null terminator.空终止符需要第七个字节。

If you have:如果你有:

char source[80],dest[80];

Initialize source, then:初始化源,然后:

strcpy(dest,source);

But if you have:但如果你有:

char *pd,*ps;

Initialize source,and malloc storage for *pd , then:*pd初始化源和 malloc 存储,然后:

strcpy(&pd,&ps);

And remember to have free(pd);并记得有free(pd); somewhere before exit(0);退出之前的某个地方(0);

According to the C Standard (6.4.5 String literals)根据 C 标准(6.4.5 字符串文字)

7 It is unspecified whether these arrays are distinct provided their elements have the appropriate values. 7 如果这些数组的元素具有适当的值,则未指定这些数组是否不同。 If the program attempts to modify such an array, the behavior is undefined.如果程序尝试修改这样的数组,则行为未定义。

You declared two pointers to string literals您声明了两个指向字符串文字的指针

char *a;
a = "aabtyn";

char *b;
b =  "mihli";

Then in this statement然后在这个声明中

strcpy(a,b);

you are trying to modify the first string literal.您正在尝试修改第一个字符串文字。 So the behaviour of the program is undefined.所以程序的行为是未定义的。

As for this statement至于这个说法

strcpy(&a,&b); 

then there is an attempt to copy the value of one pointer to another pointer.然后尝试将一个指针的值复制到另一个指针。 This call aslo has undefined behaviour.此调用也具有未定义的行为。 But if the value of the second pointer is zero-terminated then this call can be equivalent to但是如果第二个指针的值以零结尾,那么这个调用可以等价于

a = b;

So the first pointer just reassigned.所以第一个指针刚刚重新分配。 Though in any case there is undefined behaviour.尽管在任何情况下都有未定义的行为。

A valid program can look the following way一个有效的程序可以如下所示

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

int main( void ) 
{
    char s[] = "aabtyn";

    char *a = s;

    char *b;
    b =  "mihli";

    strcpy(a,b);

    printf("%s-%s\n",a,b);

    return 0;
}

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

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