函数返回值—返回局部变量

看看下面的程序的输出:<wbr style="margin:0px; padding:0px"><br style="margin:0px; padding:0px"><br style="margin:0px; padding:0px"><span style="font-family:新宋体; margin:0px; padding:0px; line-height:1.3em">#include &lt;stdio.h&gt;</span><wbr style="margin:0px; padding:0px"><br style="margin:0px; padding:0px"><span style="font-family:新宋体; margin:0px; padding:0px; line-height:1.3em">char *returnStr()</span><wbr style="margin:0px; padding:0px"><br style="margin:0px; padding:0px"><span style="font-family:新宋体; margin:0px; padding:0px; line-height:1.3em">{</span><wbr style="margin:0px; padding:0px"><br style="margin:0px; padding:0px"><span style="font-family:新宋体; margin:0px; padding:0px; line-height:1.3em"> char *p="hello world!";</span><wbr style="margin:0px; padding:0px"><br style="margin:0px; padding:0px"><span style="font-family:新宋体; margin:0px; padding:0px; line-height:1.3em"> return p;</span><wbr style="margin:0px; padding:0px"><br style="margin:0px; padding:0px"><span style="font-family:新宋体; margin:0px; padding:0px; line-height:1.3em">}</span><wbr style="margin:0px; padding:0px"><br style="margin:0px; padding:0px"><span style="font-family:新宋体; margin:0px; padding:0px; line-height:1.3em">int main()</span><wbr style="margin:0px; padding:0px"><br style="margin:0px; padding:0px"><span style="font-family:新宋体; margin:0px; padding:0px; line-height:1.3em">{</span><wbr style="margin:0px; padding:0px"><br style="margin:0px; padding:0px"><span style="font-family:新宋体; margin:0px; padding:0px; line-height:1.3em"> char *str;</span><wbr style="margin:0px; padding:0px"><br style="margin:0px; padding:0px"><span style="font-family:新宋体; margin:0px; padding:0px; line-height:1.3em"> str=returnStr();</span><wbr style="margin:0px; padding:0px"><br style="margin:0px; padding:0px"><span style="font-family:新宋体; margin:0px; padding:0px; line-height:1.3em"> printf("%s\n", str);</span><wbr style="margin:0px; padding:0px"><br style="margin:0px; padding:0px"><span style="font-family:新宋体; margin:0px; padding:0px; line-height:1.3em"> return 0;</span><wbr style="margin:0px; padding:0px"><br style="margin:0px; padding:0px"><span style="font-family:新宋体; margin:0px; padding:0px; line-height:1.3em">}</span><wbr style="margin:0px; padding:0px"><br style="margin:0px; padding:0px"><br style="margin:0px; padding:0px"><span style="font-family:新宋体; margin:0px; padding:0px; line-height:1.3em">这个没有任何问题,因为"hello world!"是一个字符串常量,存放在静态数据区,把该字符串常量存放的静态数据区的首地址赋值给了指针,所以returnStr函数退出时,该该字符串常量所在内存不会被回收,故能够通过指针顺利无误的访问。</span><wbr style="margin:0px; padding:0px"><br style="margin:0px; padding:0px"><br style="margin:0px; padding:0px"><span style="font-family:新宋体; margin:0px; padding:0px; line-height:1.3em">但是,下面的就有问题:</span><wbr style="margin:0px; padding:0px"><br style="margin:0px; padding:0px"><br style="margin:0px; padding:0px"><span style="font-family:新宋体; margin:0px; padding:0px; line-height:1.3em">#include &lt;stdio.h&gt;</span><wbr style="margin:0px; padding:0px"><br style="margin:0px; padding:0px"><span style="font-family:新宋体; margin:0px; padding:0px; line-height:1.3em">char *returnStr()</span><wbr style="margin:0px; padding:0px"><br style="margin:0px; padding:0px"><span style="font-family:新宋体; margin:0px; padding:0px; line-height:1.3em">{</span><wbr style="margin:0px; padding:0px"><br style="margin:0px; padding:0px"><span style="font-family:新宋体; margin:0px; padding:0px; line-height:1.3em"> char p[]="hello world!";</span><wbr style="margin:0px; padding:0px"><br style="margin:0px; padding:0px"><span style="font-family:新宋体; margin:0px; padding:0px; line-height:1.3em"> return p;</span><wbr style="margin:0px; padding:0px"><br style="margin:0px; padding:0px"><span style="font-family:新宋体; margin:0px; padding:0px; line-height:1.3em">}</span><wbr style="margin:0px; padding:0px"><br style="margin:0px; padding:0px"><span style="font-family:新宋体; margin:0px; padding:0px; line-height:1.3em">int main()</span><wbr style="margin:0px; padding:0px"><br style="margin:0px; padding:0px"><span style="font-family:新宋体; margin:0px; padding:0px; line-height:1.3em">{</span><wbr style="margin:0px; padding:0px"><br style="margin:0px; padding:0px"><span style="font-family:新宋体; margin:0px; padding:0px; line-height:1.3em"> char *str;</span><wbr style="margin:0px; padding:0px"><br style="margin:0px; padding:0px"><span style="font-family:新宋体; margin:0px; padding:0px; line-height:1.3em"> str=returnStr();</span><wbr style="margin:0px; padding:0px"><br style="margin:0px; padding:0px"><span style="font-family:新宋体; margin:0px; padding:0px; line-height:1.3em"> printf("%s\n", str);</span><wbr style="margin:0px; padding:0px"><br style="margin:0px; padding:0px"><span style="font-family:新宋体; margin:0px; padding:0px; line-height:1.3em"> return 0;</span><wbr style="margin:0px; padding:0px"><br style="margin:0px; padding:0px"><span style="font-family:新宋体; margin:0px; padding:0px; line-height:1.3em">}</span><wbr style="margin:0px; padding:0px"><br style="margin:0px; padding:0px"><span style="font-family:新宋体; margin:0px; padding:0px; line-height:1.3em"></span></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

"hello world!"是一个字符串常量,存放在静态数据区,没错,但是把一个字符串常量赋值给了一个局部变量(char []型数组),该局部变量存放在栈中,这样就有两块内容一样的内存,这是与前着最本质的区别,当returnStr函数退出时,栈要清空,局部变量的内存也被清空了,所以这时的函数返回的是一个已被释放的内存地址,所以打印出来的是乱码。<wbr style="margin:0px; padding:0px"><br style="margin:0px; padding:0px"><br style="margin:0px; padding:0px"><span style="font-family:新宋体; margin:0px; padding:0px; line-height:1.3em">如果函数的返回值非要是一个局部变量的地址,那么该局部变量一定要申明为static类型。如下:</span><wbr style="margin:0px; padding:0px"><br style="margin:0px; padding:0px"><br style="margin:0px; padding:0px"><span style="font-family:新宋体; margin:0px; padding:0px; line-height:1.3em">#include &lt;stdio.h&gt;</span><wbr style="margin:0px; padding:0px"><br style="margin:0px; padding:0px"><span style="font-family:新宋体; margin:0px; padding:0px; line-height:1.3em">char *returnStr()</span><wbr style="margin:0px; padding:0px"><br style="margin:0px; padding:0px"><span style="font-family:新宋体; margin:0px; padding:0px; line-height:1.3em">{</span><wbr style="margin:0px; padding:0px"><br style="margin:0px; padding:0px"><span style="font-family:新宋体; margin:0px; padding:0px; line-height:1.3em"> static char p[]="hello world!";</span><wbr style="margin:0px; padding:0px"><br style="margin:0px; padding:0px"><span style="font-family:新宋体; margin:0px; padding:0px; line-height:1.3em"> return p;</span><wbr style="margin:0px; padding:0px"><br style="margin:0px; padding:0px"><span style="font-family:新宋体; margin:0px; padding:0px; line-height:1.3em">}</span><wbr style="margin:0px; padding:0px"><br style="margin:0px; padding:0px"><span style="font-family:新宋体; margin:0px; padding:0px; line-height:1.3em">int main()</span><wbr style="margin:0px; padding:0px"><br style="margin:0px; padding:0px"><span style="font-family:新宋体; margin:0px; padding:0px; line-height:1.3em">{</span><wbr style="margin:0px; padding:0px"><br style="margin:0px; padding:0px"><span style="font-family:新宋体; margin:0px; padding:0px; line-height:1.3em"> char *str;</span><wbr style="margin:0px; padding:0px"><br style="margin:0px; padding:0px"><span style="font-family:新宋体; margin:0px; padding:0px; line-height:1.3em"> str=returnStr();</span><wbr style="margin:0px; padding:0px"><br style="margin:0px; padding:0px"><span style="font-family:新宋体; margin:0px; padding:0px; line-height:1.3em"> printf("%s\n", str);</span><wbr style="margin:0px; padding:0px"><br style="margin:0px; padding:0px"><br style="margin:0px; padding:0px"><span style="font-family:新宋体; margin:0px; padding:0px; line-height:1.3em"> return 0;</span><wbr style="margin:0px; padding:0px"><br style="margin:0px; padding:0px"><span style="font-family:新宋体; margin:0px; padding:0px; line-height:1.3em">}</span><wbr style="margin:0px; padding:0px"><br style="margin:0px; padding:0px"><br style="margin:0px; padding:0px"><span style="font-family:新宋体; margin:0px; padding:0px; line-height:1.3em">这个问题可以通过下面的一个例子来更好的说明:</span><wbr style="margin:0px; padding:0px"><br style="margin:0px; padding:0px"><br style="margin:0px; padding:0px"><span style="font-family:新宋体; margin:0px; padding:0px; line-height:1.3em">#include &lt;stdio.h&gt;</span><wbr style="margin:0px; padding:0px"><br style="margin:0px; padding:0px"><span style="font-family:新宋体; margin:0px; padding:0px; line-height:1.3em">//返回的是局部变量的地址,该地址位于动态数据区,栈里</span><wbr style="margin:0px; padding:0px"><br style="margin:0px; padding:0px"><span style="font-family:新宋体; margin:0px; padding:0px; line-height:1.3em">char *s1()</span><wbr style="margin:0px; padding:0px"><br style="margin:0px; padding:0px"><span style="font-family:新宋体; margin:0px; padding:0px; line-height:1.3em">{</span><wbr style="margin:0px; padding:0px"><br style="margin:0px; padding:0px"><span style="font-family:新宋体; margin:0px; padding:0px; line-height:1.3em"> char p[]="Hello world!";</span><wbr style="margin:0px; padding:0px"><br style="margin:0px; padding:0px"><span style="font-family:新宋体; margin:0px; padding:0px; line-height:1.3em"> printf("in s1 p=%p\n", p);</span><wbr style="margin:0px; padding:0px"><br style="margin:0px; padding:0px"><span style="font-family:新宋体; margin:0px; padding:0px; line-height:1.3em"> printf("in s1: string's address: %p\n", &amp;("Hello world!"));</span><wbr style="margin:0px; padding:0px"><br style="margin:0px; padding:0px"><span style="font-family:新宋体; margin:0px; padding:0px; line-height:1.3em"> return p;</span><wbr style="margin:0px; padding:0px"><br style="margin:0px; padding:0px"><span style="font-family:新宋体; margin:0px; padding:0px; line-height:1.3em">}</span><wbr style="margin:0px; padding:0px"><br style="margin:0px; padding:0px"><span style="font-family:新宋体; margin:0px; padding:0px; line-height:1.3em">//返回的是字符串常量的地址,该地址位于静态数据区</span><wbr style="margin:0px; padding:0px"><br style="margin:0px; padding:0px"><span style="font-family:新宋体; margin:0px; padding:0px; line-height:1.3em">char *s2()</span><wbr style="margin:0px; padding:0px"><br style="margin:0px; padding:0px"><span style="font-family:新宋体; margin:0px; padding:0px; line-height:1.3em">{</span><wbr style="margin:0px; padding:0px"><br style="margin:0px; padding:0px"><span style="font-family:新宋体; margin:0px; padding:0px; line-height:1.3em"> char *q="Hello world!";</span><wbr style="margin:0px; padding:0px"><br style="margin:0px; padding:0px"><span style="font-family:新宋体; margin:0px; padding:0px; line-height:1.3em"> printf("in s2 q=%p\n", q);</span><wbr style="margin:0px; padding:0px"><br style="margin:0px; padding:0px"><span style="font-family:新宋体; margin:0px; padding:0px; line-height:1.3em"> printf("in s2: string's address: %p\n", &amp;("Hello world!"));</span><wbr style="margin:0px; padding:0px"><br style="margin:0px; padding:0px"><span style="font-family:新宋体; margin:0px; padding:0px; line-height:1.3em"> return q;</span><wbr style="margin:0px; padding:0px"><br style="margin:0px; padding:0px"><span style="font-family:新宋体; margin:0px; padding:0px; line-height:1.3em">}</span><wbr style="margin:0px; padding:0px"><br style="margin:0px; padding:0px"><span style="font-family:新宋体; margin:0px; padding:0px; line-height:1.3em">//返回的是静态局部变量的地址,该地址位于静态数据区</span><wbr style="margin:0px; padding:0px"><br style="margin:0px; padding:0px"><span style="font-family:新宋体; margin:0px; padding:0px; line-height:1.3em">char *s3()</span><wbr style="margin:0px; padding:0px"><br style="margin:0px; padding:0px"><span style="font-family:新宋体; margin:0px; padding:0px; line-height:1.3em">{</span><wbr style="margin:0px; padding:0px"><br style="margin:0px; padding:0px"><span style="font-family:新宋体; margin:0px; padding:0px; line-height:1.3em"> static char r[]="Hello world!";</span><wbr style="margin:0px; padding:0px"><br style="margin:0px; padding:0px"><span style="font-family:新宋体; margin:0px; padding:0px; line-height:1.3em"> printf("in s3 r=%p\n", r);</span><wbr style="margin:0px; padding:0px"><br style="margin:0px; padding:0px"><span style="font-family:新宋体; margin:0px; padding:0px; line-height:1.3em"> printf("in s3: string's address: %p\n", &amp;("Hello world!"));</span><wbr style="margin:0px; padding:0px"><br style="margin:0px; padding:0px"><span style="font-family:新宋体; margin:0px; padding:0px; line-height:1.3em"> return r;</span><wbr style="margin:0px; padding:0px"><br style="margin:0px; padding:0px"><span style="font-family:新宋体; margin:0px; padding:0px; line-height:1.3em">}</span><wbr style="margin:0px; padding:0px"><br style="margin:0px; padding:0px"><span style="font-family:新宋体; margin:0px; padding:0px; line-height:1.3em">int main()</span><wbr style="margin:0px; padding:0px"><br style="margin:0px; padding:0px"><span style="font-family:新宋体; margin:0px; padding:0px; line-height:1.3em">{</span><wbr style="margin:0px; padding:0px"><br style="margin:0px; padding:0px"><span style="font-family:新宋体; margin:0px; padding:0px; line-height:1.3em"> char *t1, *t2, *t3;</span><wbr style="margin:0px; padding:0px"><br style="margin:0px; padding:0px"><span style="font-family:新宋体; margin:0px; padding:0px; line-height:1.3em"> t1=s1();</span><wbr style="margin:0px; padding:0px"><br style="margin:0px; padding:0px"><span style="font-family:新宋体; margin:0px; padding:0px; line-height:1.3em"> t2=s2();</span><wbr style="margin:0px; padding:0px"><br style="margin:0px; padding:0px"><span style="font-family:新宋体; margin:0px; padding:0px; line-height:1.3em"> t3=s3();</span><wbr style="margin:0px; padding:0px"><br style="margin:0px; padding:0px"><span style="font-family:新宋体; margin:0px; padding:0px; line-height:1.3em"> printf("in main:");</span><wbr style="margin:0px; padding:0px"><br style="margin:0px; padding:0px"><span style="font-family:新宋体; margin:0px; padding:0px; line-height:1.3em"> printf("p=%p, q=%p, r=%p\n", t1, t2, t3);</span><wbr style="margin:0px; padding:0px"><br style="margin:0px; padding:0px"><span style="font-family:新宋体; margin:0px; padding:0px; line-height:1.3em"> printf("%s\n", t1);</span><wbr style="margin:0px; padding:0px"><br style="margin:0px; padding:0px"><span style="font-family:新宋体; margin:0px; padding:0px; line-height:1.3em"> printf("%s\n", t2);</span><wbr style="margin:0px; padding:0px"><br style="margin:0px; padding:0px"><span style="font-family:新宋体; margin:0px; padding:0px; line-height:1.3em"> printf("%s\n", t3);</span><wbr style="margin:0px; padding:0px"><br style="margin:0px; padding:0px"><span style="font-family:新宋体; margin:0px; padding:0px; line-height:1.3em"> return 0;</span><wbr style="margin:0px; padding:0px"><br style="margin:0px; padding:0px"><span style="font-family:新宋体; margin:0px; padding:0px; line-height:1.3em">}</span><wbr style="margin:0px; padding:0px"><br style="margin:0px; padding:0px"><span style="font-family:新宋体; margin:0px; padding:0px; line-height:1.3em"></span></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

运行输出结果:<wbr style="margin:0px; padding:0px"><br style="margin:0px; padding:0px"><span style="font-family:新宋体; margin:0px; padding:0px; line-height:1.3em">in s1 p=0xbff92efb</span><wbr style="margin:0px; padding:0px"><br style="margin:0px; padding:0px"><span style="font-family:新宋体; margin:0px; padding:0px; line-height:1.3em">in s1: string's address: 0x80486ac</span><wbr style="margin:0px; padding:0px"><br style="margin:0px; padding:0px"><span style="font-family:新宋体; margin:0px; padding:0px; line-height:1.3em">in s2 q=0x80486ac</span><wbr style="margin:0px; padding:0px"><br style="margin:0px; padding:0px"><span style="font-family:新宋体; margin:0px; padding:0px; line-height:1.3em">in s2: string's address: 0x80486ac</span><wbr style="margin:0px; padding:0px"><br style="margin:0px; padding:0px"><span style="font-family:新宋体; margin:0px; padding:0px; line-height:1.3em">in s3 r=0x804998c</span><wbr style="margin:0px; padding:0px"><br style="margin:0px; padding:0px"><span style="font-family:新宋体; margin:0px; padding:0px; line-height:1.3em">in s3: string</span><wbr style="margin:0px; padding:0px"><br style="margin:0px; padding:0px"><br style="margin:0px; padding:0px"><span style="font-family:新宋体; margin:0px; padding:0px; line-height:1.3em">这个结果正好应证了上面解释,同时,还可是得出一个结论:字符串常量,之所以称之为常量,因为它可一看作是一个没有命名的字符串且为常量,存放在静态数据区。这里说的静态数据区,是相对于堆、栈等动态数据区而言的。静态数据区存放的是全局变量和静态变量,从这一点上来说,字符串常量又可以称之为一个无名的静态变量,因为"Hello world!"这个字符串在函数 s1和s2 中都引用了,但在内存中却只有一份拷贝,这与静态变量性质相当神似。</span></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

char *p = "abcdefg";//静态存储区

char p[] = "abcdefg"; // p本身是数组名了,数组里放的字符串,是局部变量,内容是原来的静态区域内容的拷贝!因此返回p实际上返回的局部变量地址而不是静态存储区地址,和上面不同!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值