主题:关于字符串交换的问题
jdsbj
[专家分:0] 发布于 2010-11-18 21:35:00
大家好,自己刚学C语言,关于字符串交换的问题始终想不明白,希望高人指点一下,不胜感激!
用如下函数无法实现字符串的交换:
void swap(char *m,char *n){
char *t;
t=m;
m=n;
n=t;}
但在下面这个程序中却可以实现:
#include <stdio.h>
#include <string.h>
void sort(char *name[],int n){
char *pt;
int i,j,k;
for(i=0;i<n-1;i++){
k=i;
for(j=i+1;j<n;j++)
if(strcmp(name[k],name[j])>0) k=j;
[color=0000FF]if(k!=i){
pt=name[i];
name[i]=name[k];
name[k]=pt;}[/color]
}
}
void print(char *name[],int n){
int i;
for(i=0;i<n;i++) printf("%s\n",name[i]);
}
main(){
static char *name[]={"CHINA","AMERICA","AUSTRALIA","FRANCE","GERMAN"};
int n=5;
sort(name,n);
print(name,n);
}
其中用蓝色标记的部分我觉得与上面的那个函数功能是一样的,为什么上面那个函数无法实现字符串交换,但在下面这个程序中却可以?希望高手不吝赐教。
回复列表 (共7个回复)
沙发
lanjingling1 [专家分:970] 发布于 2010-11-19 08:44:00
在swap函数中,实际上改变的是m 和n 的值,而m和n所指向的那个地址的值并没有改变,而且n 和m是函数的参数,在函数调用结束以后,m和n的内存空间就释放掉了。所以函数调用结束相当于什么也没有做。
而在sort函数中,参数传递的是数组的首地址,对形参数组的改变实际上也是对实参数组的改变,这两个数组占用的是统一内存空间
板凳
jdsbj [专家分:0] 发布于 2010-11-19 20:30:00
swap函数的形参是指针,传递的应该是地址啊?
那为什么下面这个函数可以呢?
void swap(int *m,int *n){
int t;
t=*m;
*m=*n;
*n=t;}
3 楼
windy0will [专家分:2300] 发布于 2010-11-19 20:38:00
[quote]swap函数的形参是指针,传递的应该是地址啊?
那为什么下面这个函数可以呢?
void swap(int *m,int *n){
int t;
t=*m;
*m=*n;
*n=t;}
[/quote]
那为啥不行呢?
4 楼
zhangyanrong1987 [专家分:60] 发布于 2010-11-19 23:19:00
当子函数向相应的地址写入不同的数值后,就能改变其内容。若只是改变其地址指向,当子函数结束后,其值并不会得到改变。
5 楼
lanjingling1 [专家分:970] 发布于 2010-11-20 09:02:00
void swap(int *m,int *n){
int t;
t=*m;
*m=*n;
*n=t;}
改变的是m和n所指向的内容,虽然函数退出后m和n的内存空间不存在了,但是n和m所指向的内存空间仍然存在,而且内容已经改变
6 楼
jdsbj [专家分:0] 发布于 2010-11-20 10:17:00
现在想明白了,谢谢你了。
7 楼
dfsafds [专家分:30] 发布于 2010-12-03 13:08:00
Nowadays, most [url=http://www.mmopowerlevel.net]wow power leveling[/url] of the male singles turning [url=http://www.mogxe.com/PowerLevel.php?gid=1]wow power leveling[/url] towards finding Asian wives for them selves. The reason being Asian wives are considered to be the world’s best and loving wives. There are so many examples of American [url=http://www.mmopowerlevel.net/powerlist.php?fid=688]wow power leveling[/url] males married to Asian females. Asian girls never forget their culture, respect for elders and tradition, no [url=http://www.mmopowerlevel.net/gamelist.php?fid=4044]EVE ISK[/url] matter wherever they go. They offer [url=http://www.mmopowerlevel.net/gamelist.php?fid=7656]aion kina[/url] complete support and help to their spouse. Great support, encouragement, peace, loves and care is what demanded by every single male all around the world. She [url=http://www.mogxe.com]buy wow gold[/url] will make your day and night superb and full of beautiful moments. Asian women can also prepare food you love to eat. What [url=http://www.mmopowerlevel.net/buy.php]gold in wow[/url] are the main traits that make Asian wives better than western wives?
Asian wives have powerful [url=http://www.mmopowerlevel.net/buy.php]wow gold eu[/url] sexual attraction and appeal:
An Asian female is a combination of exotic [url=http://www.mmopowerlevel.net/buy.php]wow gold cheap[/url] oriental features, tough bodies, petite structure, mind blowing skin and silky hair. These [url=http://www.mmopowerlevel.net/powerlist.php?fid=7422]cheap aion power leveling[/url] are some things which every male wants in his wife.
我来回复