交换数值(函数传值问题)

作者在 2011-08-23 16:34:01 发布以下内容
# include <stdio.h>

int main(void)
{
    void swap(int a, int b);
    int a = 2, b = 3;
    
    swap(a, b);
    printf("%d\n%d\n", a, b);

    return 0;
}

void swap(int a, int b)
{
    int c;
    c = a;
    a = b;
    b = c;
}
不用编译器,你想想会输出什么?
正确答案:
2
3
你错了吗?知道问什么错了吗?
我个人看法是:
void swap(int a, int b)
{
    int c;
    c = a;
    a = b;
    b = c;
}
函数定义里面的a,b均是函数定义里面的变量,生命周期仅仅在函数定义里面,即:只在函数定义里面有效。出来void swap(int a, int b){.......}就挂掉了~
如果还是不能理解那么可以把函数声明改成:
void swap(int m, int n);
(当然函数声明可以不改,因为原函数声明里的a,b的声明周期仅仅在函数声明的“(”与“)”之间)。
函数定义修改为:
void swap(int m, int n)
{
    int c;
    c = m;
    m = n;
    n = c;
}
那么,相信就更容易理解了~
 
如果你想交换两个数值。可以用一下代码:
# include <stdio.h>

int main(void)
{
    void swap(int *a, int *b);////////////
    int a = 2, b = 3;
    
    swap(&a, &b);//////////////////注意
    printf("%d\n%d\n", a, b);

    return 0;
}

void swap(int *a, int *b)/////////////////
{
    int c;
    c = *a;        /////
    *a = *b;    /////
    *b = c;        /////
}
输出:
3
2
C语言问题 | 阅读 1035 次
文章评论,共12条
edwardflee
2011-08-24 10:46
1
函数还没学完,大致理解你的意思,不过想实现交换功能的话,把printf挪到swap函数里不就可以了吗?<br />
# include &lt;stdio.h&gt;<br />
<br />
int main(void)<br />
{<br />
&nbsp; &nbsp; void swap(int a, int b);<br />
&nbsp; &nbsp; int a = 2, b = 3;<br />
 <br />
&nbsp; &nbsp;swap(a, b);<br />
&nbsp; &nbsp; return 0;<br />
}<br />
<br />
void swap(int a, int b)<br />
{<br />
&nbsp; &nbsp; int c;<br />
&nbsp; &nbsp; c = a;<br />
&nbsp; &nbsp; a = b;<br />
&nbsp; &nbsp; b = c;<br />
&nbsp; &nbsp; printf(&quot;%d\n%d\n&quot;, a, b);<br />
}
laznrbfe(作者)
2011-08-24 15:58
2
<div class="quote"><span class="q"><b>edwardflee</b>: 函数还没学完,大致理解你的意思,不过想实现交换功能的话,把printf挪到swap函数里不就可以了吗?<br />
# include &lt;stdio.h&gt;<br />
<br />
int main(void)<br />
{<br />
&nbsp; &nbsp; void swa</span></div>你的想法不对。主函数中的a,b值没有改变。你可以在return 0;前加printf(&quot;\na=%d,b=%d&quot;,a,b);你就可以看出a,b值没有改变。
hollybest
2011-08-24 17:36
3
<img src="image/face/3.gif" class="face">确实如此,呵呵呵
laznrbfe(作者)
2011-08-25 19:20
4
<div class="quote"><span class="q"><b>hollybest</b>: <img src="image/face/3.gif" class="face">确实如此,呵呵呵</span></div><img src="image/face/2.gif" class="face">
迷途精灵
2011-08-27 22:54
5
值传递 似乎不行哦&nbsp;&nbsp;地址传递<br />
void swap(int * p, int * q) <br />
{<br />
int t;<br />
t = *p;<br />
*p = *q;<br />
*q = t; <br />
}<br />
引用函数时<br />
swap(&amp;a, &amp;b)
laznrbfe(作者)
2011-08-27 23:21
6
<div class="quote"><span class="q"><b>迷途精灵</b>: 值传递 似乎不行哦&nbsp;&nbsp;地址传递<br />
void swap(int * p, int * q) <br />
{<br />
int t;<br />
t = *p;<br />
*p = *q;<br />
*q = t; <br />
}<br />
引用函数时<br />
swap(&amp;a, &amp;b)</span></div>我又运行了几次,没有发现错误。我用的编译器是VC++6.0。你可以运行一下,如果还有问题,我们再继续讨论。谢谢。
迷途精灵
2011-08-27 23:42
7
我也用的这个软件&nbsp;&nbsp;这种互换的方法是对的 那就没问题啦
laznrbfe(作者)
2011-08-28 08:13
8
<div class="quote"><span class="q"><b>迷途精灵</b>: 我也用的这个软件&nbsp;&nbsp;这种互换的方法是对的 那就没问题啦</span></div><img src="image/face/2.gif" class="face">嗯。
循个
2011-08-28 22:48
9
<img src="image/face/2.gif" class="face">
laznrbfe(作者)
2011-08-29 16:21
10
<div class="quote"><span class="q"><b>循个</b>: <img src="image/face/2.gif" class="face"></span></div><img src="image/face/2.gif" class="face">
edwardflee
2011-08-31 14:56
11
开始学指针,终于明白了,呵呵。干嘛改头像?原来那个钓鱼的很帅啊
laznrbfe(作者)
2011-08-31 15:43
12
<div class="quote"><span class="q"><b>edwardflee</b>: 开始学指针,终于明白了,呵呵。干嘛改头像?原来那个钓鱼的很帅啊</span></div><img src="image/face/2.gif" class="face">怕同学找到我~呵呵~
游客请输入验证码
浏览48200次