+ -
当前位置:首页 → 问答吧 → 为什么运行显示NULL,而未被改写成I use?

为什么运行显示NULL,而未被改写成I use?

时间:2011-12-14

来源:互联网

#include<iostream>
#include<stdio.h>
using namespace std;

void main()
{ void read(char *cp );
char string[]="NULL";
  read(string);
  cout<<(int)string;
  getchar();
}
===========================================
#include<iostream>
#include<stdio.h>
using namespace std;

void read(char *cp )
{ cp ="I use";
}

作者: holidayvocation   发布时间: 2011-12-14

指针参数要修改成二级指针,改动的请见注释
C/C++ code

#include<iostream>
#include<stdio.h>
using namespace std;

void main()
{ void read(char **cp ); // 改动1
char string[]="NULL";
  read(&string);         // 改动2
  cout<<(int)string;
  getchar();
}
===========================================
#include<iostream>
#include<stdio.h>
using namespace std;

void read(char **cp ) // 改动3
{ *cp ="I use";       // 改动4
}


作者: seucs   发布时间: 2011-12-14

还有发现你一个问题,你给的是数组首地址,应该要改成指针,不然极有可能导致运行时的错误
C/C++ code

#include<iostream>
#include<stdio.h>
using namespace std;

void main()
{ void read(char **cp ); // 改动1
char *string="NULL";     // 在上面的基础上,还要改动这里
  read(&string);         // 改动2
  cout<<(int)string;
  getchar();
}
===========================================
#include<iostream>
#include<stdio.h>
using namespace std;

void read(char **cp ) // 改动3
{ *cp ="I use";       // 改动4
}


作者: seucs   发布时间: 2011-12-14

貌似找您那样改编译通不过。。。&string实参和形参不兼容 而且同为一级指针不能修改貌似变成二级指针还不一样?
引用 1 楼 seucs 的回复:

指针参数要修改成二级指针,改动的请见注释
C/C++ code

#include<iostream>
#include<stdio.h>
using namespace std;

void main()
{ void read(char **cp ); // 改动1
char string[]="NULL";
read(&amp;string); // 改动2
c……

作者: holidayvocation   发布时间: 2011-12-14

这个函数的本意是想通过CP指针将string的地址传进去,然后再次基础上覆盖了NULL,但是在string地址值穿进去后,地址竟然变了,这是为什么呢
引用 1 楼 seucs 的回复:

指针参数要修改成二级指针,改动的请见注释
C/C++ code

#include<iostream>
#include<stdio.h>
using namespace std;

void main()
{ void read(char **cp ); // 改动1
char string[]="NULL";
read(&amp;string); // 改动2
c……

作者: holidayvocation   发布时间: 2011-12-14

热门下载

更多