C++为什么 strcpy() 不需要通过引用调用
我的家庭作业有很多问题。有人会问为什么 strcpy() 函数不需要 CString 的引用运算符调用。我已经把这本书翻了很多遍,但我终其一生都找不到答案。谁能帮我解释一下吗?
它是一个数组,所以我认为您需要通过引用进行调用。
I have a homework assignment with a number of questions. One is asking why the strcpy() function doesn't need the call by reference operator for CStrings. I've looked through the book numerous times and I can't, for the life of me, find the answer. Can anyone help explain this to me?
It is an array of sorts so I would think you would need the call by reference.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
strcpy()
接受一个指向 char 的指针。因此,您不传递“字符串”作为参数,而仅传递其第一个字符的地址。
所以基本上你有这样的事情:
传递指针很快。在 32 位架构上,无论指向的字符串的长度是多少,指针都占用 32 位。
strcpy()
takes a pointer to char.Thus you don't pass the "string" as a parameter but only the address of its first character.
So basically you have something like this:
Passing a pointer is fast. On a 32 bits architecture, a pointer takes 32 bits, whatever the length of the pointed string.
如果您指的是
class CString
,那么换句话说,问题会问您:为什么会编译?
答案是,因为
class CString
定义了一个运算符 const char*,因此可以转换为 strcpy 的第二个参数的类型。我不确定这是否是你的意思。
If you mean
class CString
, then in other words the question asks you:Why does this compile?
The answer is, because
class CString
defines an operator const char* and therefore can be converted to the type ofstrcpy
's second argument.I 'm not sure if this is what you mean though.
这主要是一个术语问题。
当被调用函数获取该块的副本时,“对象”(我使用该术语来设计“一块 RAM”)将通过值传递。当被调用函数获得访问唯一块的方法时,它会通过引用传递。
考虑一下:
这里,函数
f()
修改了x
,但那是它自己的x
,一个包含内容副本的变量g()
的y
变量。f()
对其x
所做的操作不会影响g()
的y
包含的内容。然后变量按值传递。C++ 有一个引用的概念,如下所示:
这里,特殊的构造(带有“
&
”)指示 C++ 编译器发挥一些隐藏的技巧,以便x
f() 所知道的 > 实际上是g()
使用的y
变量的一种别名。只有一个变量:x
和y
指定同一块 RAM。这里的变量是通过引用传递的。现在,考虑一个“C 字符串”。在 C(和 C++)中,字符串只是一堆
char
值,其中最后一个值为 0(这是传统的字符串终止符)。代码不直接处理字符串;它使用指针。指针是一个实际指定 RAM 中位置的值。指针不是字符串;指针是一种数字(通常为 32 或 64 位,取决于处理器类型和操作系统),它告诉 RAM 中字符串的第一个char
的位置。因此,当您调用strcpy()
时,您实际上给了它指针(一个用于目标缓冲区,一个用于源字符串)。这些指针未修改:源字符串和目标缓冲区在此过程中未移动;仅字符串的内容被复制到目标缓冲区中。因此,strcpy() 不需要访问调用者代码中包含指针的变量。
strcpy()
只需要知道目标缓冲区和源字符串在 RAM 中的位置。将指针值的副本提供给strcpy()
就足够了。因此,这些指针是按值传递的。请注意,在存在指针的情况下,需要考虑两个对象:包含指针的变量和所指向的 RAM 块。指针本身按值传递(
strcpy()
接收其自己的变量副本,其中包含指向目标缓冲区的指针)。我们可以说,指向的 RAM 块(目标缓冲区)是“通过引用传递”的,因为它在进程中不会重复,并且被调用的函数 (strcpy()
) 可以修改它。这里的要点是,术语“引用”具有两个不同的含义:C++ 语法含义:“引用”指定具有“
&
”的特殊结构语言理论的正式含义:“引用”指定一种间接指定值的方式,以便调用者和被调用者可以以不同的名称访问同一块 RAM。从这个意义上来说,通过值传递一个指向被调用函数的指针相当于通过引用传递指针所指向的RAM块。
C++“引用”(第一个含义)是“通过引用”(第二个含义)传递变量的语法方式。
This a problem of terminology, mostly.
An "object" (I use the term as designing "a chunk of RAM") is passed by value when the called function gets a copy of the chunk. It is passed by reference when the called function gets a way to access the one and only chunk.
Consider this:
Here, the function
f()
modifiesx
, but that is its ownx
, a variable which contains a copy of the contents of they
variable ofg()
. Whatf()
does with itsx
does not impact what they
ofg()
contains. The variable is then passed by value.C++ has a notion of reference which goes like this:
Here, the special construction (with the "
&
") instructs the C++ compiler to play some hidden tricks so that thex
known byf()
is actually a kind of alias on they
variable used byg()
. There is only one variable: thex
and they
designate the same chunk of RAM. The variable is here passed by reference.Now, consider a "C string". In C (and C++), a string is just a bunch of
char
values, the last of which having value 0 (this is the conventional string terminator). The code does not handle strings directly; it uses pointers. A pointer is a value which actually designates an emplacement in RAM. The pointer is not the string; the pointer is a kind of number (usually on 32 or 64 bits, it depends on the processor type and the operating system) which tells where in RAM is the firstchar
of the string. So when you callstrcpy()
you actually give it pointers (one for the destination buffer, one for the source string). Those pointers are unmodified: the source string and the destination buffers are not moved in the process; only the contents of the string are copied into the destination buffer.Hence,
strcpy()
needs not have access to the variables which contain the pointers in the caller code.strcpy()
only needs to know where the destination buffer and the source strings are in RAM. Giving a copy of the pointer values tostrcpy()
is enough. Hence, those pointers are passed by value.Note that in the presence of pointers, there are two objects to consider: the variable which contains the pointer, and the chunk of RAM which is pointed to. The pointer itself is passed by value (
strcpy()
receives its own copy of the variable which contains the pointer to the destination buffer). We can say that the pointed-to chunk of RAM (the destination buffer) is "passed by reference" since it is not duplicated in the process and the called function (strcpy()
) can modify it. The point here is that the term "reference" has two distinct meanings:The C++ syntax meaning: "reference" designates the special construction with the "
&
" that I have described above.The language theory formal meaning: "reference" designates a way by which a value is indirectly designated, so that caller and callee may access the same chunk of RAM under distinct names. With that meaning, passing by value a pointer to a called function is equivalent to passing by reference the chunk of RAM to which the pointer points.
A C++ "reference" (first meaning) is a syntaxic way to pass "by reference" (second meaning) a variable.
好吧,如果您指的是 c 字符串 (char*),则不需要通过引用进行调用,因为字符串本身就是一个指针。因此字符串复制知道从哪里复制字符串到哪里/从哪里复制字符串。
Well in the case you mean c-strings (char*) you don't need a call by reference because the string itself is a pointer. So string copy knows where to/from where to copy the string.
因为 strcpy 与作为指针的 char* 一起使用。指针按值传递,strcpy 使用该指针访问目标字符串中的各个字符并更改它们。与按值传递整数相比,该函数无法更改原始整数。
了解 char* 字符串与整数的不同之处对于您在 C++ 课程中不至于发疯至关重要。你的教授让你面对它,做得很好。
Because strcpy works with char* which are pointers. The pointer is passed by value, and strcpy uses that pointer to access the indiviual characters in the target string and change them. Compare that to passing an integer by value - the function can't change the original integer.
Understanding how char* strings are not like integers is vital to you not going crazy during your C++ course. Well done for your prof making you face it.
因为在C中调用函数时,数组是作为第一个元素的地址传递的,相当于按引用调用。
请参阅 Peter Van Der Linden Expert C 编程《深层秘密》一书。
Because in C when calling functions, arrays are passed as the address of the first element, which is equivalent of calling by reference.
See Peter Van Der Linden Expert C programming, Deep secrets book.
自动类型转换,我猜他们正在寻找答案。看看这个出现可能会给你一些帮助。
automatic type conversion, is the answer I guess they're looking for. Looking that turn up might give you some help.