由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - c++ 程序一问
相关主题
写了一个find kth number in 2 sorted arrays的code 请大牛看one c++ question
新手问个C++(Thinking in C++ source code)C的argc问题
请教C/C++小为什么我这段简单的程序segment fault
问个c++的问题这题哪错了?
C++ object size一问问两个题
问一道kth smallest element的题目一道C语言题
一题懒得写了,想练手的就写写贴在这里吧
Interview questions, Bloomberg问一个c++ 函数指针的问题
相关话题的讨论汇总
话题: char话题: test话题: pointer话题: memset话题: 10
进入JobHunting版参与讨论
1 (共1页)
B***n
发帖数: 84
1
下面程序的输出结果是什么?为什么不是期望的结果 10个A?
#include
#include
using namespace std;
void test( char *p )
{
p = new char[10];
memset(p, 'A', 10);
}
int main()
{
char *k;
test(k);
cout << k << endl;
return 0;
}
E********a
发帖数: 124
2

p is unchanged, since it's passed by value to test(). I know it seems to
you the parameter is pointer, bla bla
bla

【在 B***n 的大作中提到】
: 下面程序的输出结果是什么?为什么不是期望的结果 10个A?
: #include
: #include
: using namespace std;
: void test( char *p )
: {
: p = new char[10];
: memset(p, 'A', 10);
: }
: int main()

l*******o
发帖数: 791
3
楼上说的对。
问题出现在你的test函数,test函数接受的是一个以值传递的指针。比如说一个经典的
swap(int a, int b)的例子,必须写成swap(int * a, int * b)或者引用才能改变外
界传入函数参数的值。同理如果你想改变一个指针的值,你需要用对一个指向这个指针
的指针进行操作,
所以test必须写成
void test (char * * p)
{
*p = new char[10];
memset((*p), 'A', 10);
}
然后main函数写成
int main(int argc, char *argv[])
{
char *k;

test(&k);
cout << k << endl;
}
不过一般倾向于使用引用即reference这样你就不会混淆,reference版本如下
void test( char * & p )
{
p = new char[10];
memset(p, 'A', 10);
int main(int argc, char *argv[])
{
char *k;

test(k);
cout << k << endl;

}
值得注意的是,即使这么做你能输出10个A。但是后面会有一些垃圾字符,因为你没有
在字符后加终止符号'\0';
所以memset那里最好写成,对于引用
p = new char[11];
memset(p, 'A', 10);
p[10]='\0';
对于指针的指针
*p = new char[11];
memset((*p), 'A', 10);
(*p)[10]='\0';
你明白了么
w******f
发帖数: 620
4
Another way is you could just change the test to return char * and return
the p, it will work. But you need remember to delete the pointer after you
finished.
Basically when you pass the pointer into the function, in function stack
memory, it will copy the value of pointer, it don't change anything about
the pointer itself, it could change the value this pointer pointer to, but
if you dynamic allocate memory and assign to p, then p hold different value,
it does nothing to do with the original P.
s*****n
发帖数: 5488
5
use **p. you just pass in an address, which will not be changed after
returning.

【在 B***n 的大作中提到】
: 下面程序的输出结果是什么?为什么不是期望的结果 10个A?
: #include
: #include
: using namespace std;
: void test( char *p )
: {
: p = new char[10];
: memset(p, 'A', 10);
: }
: int main()

x**********l
发帖数: 271
6
不好意思,我还是没明白 main() 中调用test(),为什么是&k
void test (char * * p)
{
*p = new char[10];
memset((*p), 'A', 10);
}
然后main函数写成
int main(int argc, char *argv[])
{
char *k;

test(&k); ------------------------->为什么要传递 &k, 而不是k 呢
cout << k << endl;
}

【在 l*******o 的大作中提到】
: 楼上说的对。
: 问题出现在你的test函数,test函数接受的是一个以值传递的指针。比如说一个经典的
: swap(int a, int b)的例子,必须写成swap(int * a, int * b)或者引用才能改变外
: 界传入函数参数的值。同理如果你想改变一个指针的值,你需要用对一个指向这个指针
: 的指针进行操作,
: 所以test必须写成
: void test (char * * p)
: {
: *p = new char[10];
: memset((*p), 'A', 10);

d**e
发帖数: 6098
7
因为test的signature的参数是 char **p
所以传入时就用指针的地址。

【在 x**********l 的大作中提到】
: 不好意思,我还是没明白 main() 中调用test(),为什么是&k
: void test (char * * p)
: {
: *p = new char[10];
: memset((*p), 'A', 10);
: }
: 然后main函数写成
: int main(int argc, char *argv[])
: {
: char *k;

x**********l
发帖数: 271
8
哦,谢谢

【在 d**e 的大作中提到】
: 因为test的signature的参数是 char **p
: 所以传入时就用指针的地址。

k*******d
发帖数: 1340
9
or you could use reference to pointer, same thing
1 (共1页)
进入JobHunting版参与讨论
相关主题
问一个c++ 函数指针的问题C++ object size一问
请教一个入门级的C的指针问题问一道kth smallest element的题目
amazon的那道题目一题
one C++ questionInterview questions, Bloomberg
写了一个find kth number in 2 sorted arrays的code 请大牛看one c++ question
新手问个C++(Thinking in C++ source code)C的argc问题
请教C/C++小为什么我这段简单的程序segment fault
问个c++的问题这题哪错了?
相关话题的讨论汇总
话题: char话题: test话题: pointer话题: memset话题: 10