为什么一个字符串拷贝函数只是分配指针不工作? [英] Why is a string copy function just assigning the pointer not working?

查看:121
本文介绍了为什么一个字符串拷贝函数只是分配指针不工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑这个实施的strcpy

 无效my_strcpy(字符目标[],炭源[])
{
   目标=来源;
}INT主要(无效)
{
   焦炭目标[20];
   炭源[] =源字符串;   my_strcpy(目标,源);
   输出(目标:%S,目标);   返回0;
}

这不工作,它让我质疑我在C字符串和数组的理解。

下面是我的推理:目标其实只是指向数组的指针的第一个元素,即。 目标==&放大器;目标[0] 来源==&安培;源[0] 。当我设置目标=源,我指着指针目标在相同的内存地址在指针对准。

现在,当我的printf 目标,也应该打印源字符串。但事实并非如此。

有人能解释为什么吗?


解决方案

 无效my_strcpy(字符目标[],炭源[])
{
   目标=来源;
}

你在这里做什么,因为你写的,通过指针的功能。但本身传递的指针按值的,因此函数有它的这些指针的本地副本。在目标里面的功能便会立即停止上课现有函数退出。

要实际修改指针的函数,你就必须通过的指针,该指针的,例如像这样的:

 无效my_strcpy(字符*目标[],炭源[])
{
   *目标=来源;
}

请注意,这仍然不会与你的程序工作,因为在的main()声明数组。声明的数组的地址不能被改变。这是因为数组的的一样的指针(因为你经常可以阅读写得不好Ç教程),但仅仅是隐式转换为一个指向它的第一个元素,当你把它传递给函数。所以,写的char *目标的main(),这个的工作,呼吁像 my_strcpy功能(安培;目标,源);

另外请注意,这是在没办法拷贝,所以你的函数的命名是语义错误的。一个真正的复制功能应该是这样的:

 无效my_strcpy(字符*目标,为const char *源)
{
   而(* ++目标= *源++);
}

复制单个字符,直到触及 \\ 0 字符(当时的前pression将评估为 0 和while循环将停止)。这个超级简单的实现叶的负担,为目标提供足够的存储,并确保实际上 0终结的调用者,但它确实或多或少什么标准C库的的strcpy()一样。

修改:我只是改变你的函数签名位 - 第一个变化是一个品味的问题,替换标识符[] *标识符。我喜欢这样做,因为这是内部反正发生,所以它使更清晰什么功能,其实需要。第二个是增加了常量在适当情况下:您的功能不会改变什么指向,所以你应该更加明确 - 这样编译器可以捕捉更多的错误,你

Consider this implementation of strcpy:

void my_strcpy(char target[], char source[])
{
   target = source;
}

int main(void)
{
   char target[20];
   char source[] = "Source String";

   my_strcpy(target, source);
   printf("Target: %s", target);

   return 0;
}

This doesn't work and its made me question my understanding of strings and arrays in C.

Here's my reasoning: target and source are really just pointers to the first elements of arrays, ie. target == &target[0] and source == &source[0]. When I set target = source, I'm pointing the pointer target at the same memory address where the pointer source is pointed at.

Now when I printf target, it should also print "Source String". But it doesn't.

Can someone explain why?

解决方案

void my_strcpy(char target[], char source[])
{
   target = source;
}

What you do here is, as you wrote, pass the pointers to the function. But the pointers themselves are passed by value, so the function has it's local copies of these pointers. The target inside the function stops existing as soon es the function exits.

To actually modify pointers in a function, you would have to pass a pointer to this pointer, e.g. like this:

void my_strcpy(char *target[], char source[])
{
   *target = source;
}

Note that this still wouldn't work with your program, because in main() you declare an array. The address of a declared array cannot be changed. This is because an array is not the same as a pointer (as you can often read in poorly written C tutorials) but is just implicitly converted to a pointer to its first element when you pass it to a function. So, writing char *target in your main(), this would work, calling the function like my_strcpy(&target, source);.

Also note that this is in no way a copy, so the naming of your function is semantically wrong. A real copy function would look like this:

void my_strcpy(char *target, const char *source)
{
   while (*target++ = *source++);
}

copying the individual characters until hitting a \0 character (then the expression would evaluate to 0 and the while loop would stop). This super-simple implementation leaves the burden to provide enough storage for target and to make sure source is actually 0-terminated to the caller, but it is indeed more or less what the standard C library's strcpy() does.

edit: I just changed your function signature a bit -- the first change is a matter of taste, replacing identifier[] by *identifier. I like to do this because this is what happens internally anyways, so it makes clearer what the function actually takes. The second one is adding a const where appropriate: Your function never changes what is pointed to by source, so you should make this explicit -- this way the compiler can catch more errors for you.

这篇关于为什么一个字符串拷贝函数只是分配指针不工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆