一个关于指针的程序

幻梦雷音 2011-04-08 02:15:36
#include"stdio.h"
char *Msg="HELLO";
void ChMsg(char *message)
{
message=NULL;
}
int main()
{
ChMsg(Msg);
if(!Msg)
printf("change ok");
else
printf("sorry");
getch();
return 0;
}
此时为什么输出的是“sorry”?
怎样在只改变ChMsg函数的情况下使程序输出“change ok”?

...全文
167 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
幻梦雷音 2011-04-10
  • 打赏
  • 举报
回复
嗯,正解,非常感谢,现在在传递上不晕了。
幻梦雷音 2011-04-10
  • 打赏
  • 举报
回复
嗯,是值传递搞出的错误。10楼是错误的。谢谢大侠们的回帖。此贴已结。
漫步者、 2011-04-10
  • 打赏
  • 举报
回复


//版本(1)

#include <stdio.h>
#include <conio.h>
char *Msg="HELLO";
void ChMsg(char *&message){
//
//你这是将message指向了Msg,然后又将message给赋上NULL
//函数返回,message消失,但是并没有影响到Msg
//
message=NULL;
Msg=message;//添加这句OK了;
}
int main(){
ChMsg(Msg);
if(!Msg)

printf("change ok");
else
printf("sorry");
getch();
return 0;
}



//版本(2)

#include <stdio.h>
#include <conio.h>
char *Msg="HELLO";
void ChMsg(char *&message){
//
//你这是将message指向了Msg,然后又将message给赋上NULL
//函数返回,message消失,但是并没有影响到Msg
//
message=NULL;
}
int main(){
ChMsg(Msg);
if(!Msg)

printf("change ok");
else
printf("sorry");
getch();
return 0;
}

//版本(3)

#include <stdio.h>
#include <conio.h>
char *Msg="HELLO";
void ChMsg(char **message){
//
//你这是将message指向了Msg,然后又将message给赋上NULL
//函数返回,message消失,但是并没有影响到Msg
//
message=NULL;
}
int main(){
ChMsg(&Msg);
if(!Msg)

printf("change ok");
else
printf("sorry");
getch();
return 0;
}

///给你写了3个样本
//楼主你再看下,10L的没有错误。
lccccccx 2011-04-08
  • 打赏
  • 举报
回复

#include"stdio.h"

char *Msg="HELLO";

void ChMsg(char **message)
{
*message=NULL;
}
int main()
{
ChMsg(&Msg);
if(!Msg)
{
printf("change ok\n");
}
else
{
printf("sorry\n");
}

return 0;
}

貌似这就行
独酌逸醉 2011-04-08
  • 打赏
  • 举报
回复
可以结贴了!
handoudouhao 2011-04-08
  • 打赏
  • 举报
回复
楼上说了很多了,你只要把函数传值看明白就ok了,这些东西不难理解,懂了后再看这个程序,一目了然!
自律则自由 2011-04-08
  • 打赏
  • 举报
回复

void ChMsg(char *&message)//函数改变的是指针的副本,改为传指针的引用
{
message=NULL;
}
--------------------------------
正解
漫步者、 2011-04-08
  • 打赏
  • 举报
回复

#include <stdio.h>
#include <conio.h>
char *Msg="HELLO";
void ChMsg(char* message){
//
//你这是将message指向了Msg,然后又将message给赋上NULL
//函数返回,message消失,但是并没有影响到Msg
//
message=NULL;
Msg=message;//添加这句OK了;
}
int main(){
ChMsg(Msg);
if(!Msg)

printf("change ok");
else
printf("sorry");
getch();
return 0;
}
漫步者、 2011-04-08
  • 打赏
  • 举报
回复

#include <stdio.h>
#include <conio.h>
char *Msg="HELLO";
void ChMsg(char* message){
//
//你这是将message指向了Msg,然后又将message给赋上NULL
//函数返回,message消失,但是并没有影响到Msg
//
message=NULL;
Msg=message;//添加这句OK了;
}
int main(){
ChMsg(Msg);
if(!Msg)

printf("change ok");
else
printf("sorry");
getch();
return 0;
}
touda2 2011-04-08
  • 打赏
  • 举报
回复
Msg根本没被改变
binggou8993 2011-04-08
  • 打赏
  • 举报
回复
void ChMsg(char **message)

void ChMsg(char *&message)
都可以
都是正解
mao_pu_hua 2011-04-08
  • 打赏
  • 举报
回复
c语言是值传递。好好研读下面的例子,每个书都有的
chg(int *a, int *b)
{
int *tmp;
tmp = a;
a = b;
b = tmp;
}
这个是错误的,你想下为什么

chg(int *a, int *b)
{
int tmp;
tmp = *a;
*a = *b;
*b = tmp;
}
这个才是对的,你再想下。理解了这个,你那个就很容易了

关键就是值传递

qbwjly 2011-04-08
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 last_c_ 的回复:]
void ChMsg(char **message)
{
*message=NULL;
}

ChMsg(&Msg);
[/Quote]
正解
yuppy 2011-04-08
  • 打赏
  • 举报
回复
传一个指向msg地址的指针给函数 就ok了
jialejiahi 2011-04-08
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 last_c_ 的回复:]

void ChMsg(char **message)
{
*message=NULL;
}

ChMsg(&Msg);
[/Quote]
正解
hzhxxx 2011-04-08
  • 打赏
  • 举报
回复


ChMsg(Msg);

这个并没有修改 Msg 的指针,实际是修改了一个栈变量

看看函数的调用过程吧
LaSt_C_ 2011-04-08
  • 打赏
  • 举报
回复
void ChMsg(char **message)
{
*message=NULL;
}

ChMsg(&Msg);
cap77 2011-04-08
  • 打赏
  • 举报
回复

void ChMsg(char *&message)//函数改变的是指针的副本,改为传指针的引用
{
message=NULL;
}

69,373

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧