⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 passbyref2.html

📁 this is a mirrored site c-faq. thought might need offline
💻 HTML
字号:
<html><!-- Mirrored from c-faq.com/ptrs/passbyref2.html by HTTrack Website Copier/3.x [XR&CO'2008], Sat, 14 Mar 2009 08:02:58 GMT --><head><title></title></head><body>[This is a mail message I sent to someone who was wondering how,if C uses ``pass by value'',a function could modify a caller's passed array.]<p>From: scs@eskimo.com (Steve Summit)<br>Subject: Re: Functions<br>Date: Mon, 27 May 1996 08:50:17 -0700 (PDT)<br>Message-Id: &lt;199605271550.IAA07326@mail.eskimo.com&gt;<br>In-Reply-To: &lt;4o4p05$5s7@oly.olympic.net&gt;<p>&gt; I realize that only the values of the variables are passed to the function<br>&gt; on a stack, which makes it impossible for the calling function to alter<br>&gt; the values in the main function. Thus why is it possible for strcat() to<br>&gt; alter a string value in the main function, from the strcat function. <p>There are two ways of looking at this.<p>The simplified way (which is also ever-so-slightly incorrect) isto say that arrays are an exception to the rule that functionarguments are passed by value.  You can say that strcat() acceptstwo arrays, and that arrays are passed by reference, in which casethe function <em>can</em> modify the contents of the array back up therein the caller.<p>The completely correct way to explain it (which is also somewhatconfusing at first) is to say that arrays are never passed tofunctions at all.  Instead, a pointer to the array's firstelement is passed.  (In fact, you will usually see strcat()documented as accepting two pointers to char.)  The functionreceives a copy of the pointer, as usual, so it cannot modify thepointer up in the caller.  <em>But</em>, the copied pointer it receivesobviously points to the same place (in this case, to the samearray containing the same string) as the original, so thefunction <em>can</em> modify the memory (i.e. the string) pointed to bythe pointer.</body><!-- Mirrored from c-faq.com/ptrs/passbyref2.html by HTTrack Website Copier/3.x [XR&CO'2008], Sat, 14 Mar 2009 08:02:58 GMT --></html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -