C++ forums 的鬼佬对话 Pointers and functions
Okay so I've been wrestling with this for two days for a total of well over 20 hours. I've read through好的 我为这个耗二日多出20小时.
the tutorial on this site and five other sites, watched a YouTube vid about it and read my book.
我通过这个网站和四个网站去理解.上YouTube看这个,而且还看书
A few times over. I normally wouldn't ask, but I have this week to learn about pointers and classes
时间过去。我不说什么,但是我在周未学习指针和类,更多关注类与继承.
and more about classes and inheritance. That's about 150 pages in my book. I am getting a little stressed
系我书本150页。我依然感得不知所措
over this.
I've been sitting with this up for 20 minutes and I still don't know how to concretize what I am asking
我一直坐到20分钟,我仍然不知道怎样表达
for into a question. Any example involving a function with pointers (including one as an argument for
一些例子包括函数里的指针和我希望一步步做什么整合到字符类型的变值
that function) and char type variables with a nice step by step walkthrough on what's what will do... I hope.
For starters, take a look at this POC-code here.
#include <iostream>
void converter(const char *aString)
{
const char *charPointer = "Hello!";
charPointer = aString;
}
int main()
{
const char *myString = "Good-bye.";
converter(myString);
std::cout << myString;
}
I want myString to print out "Hello!" but it prints out "Good-
我想这行输出"Hello!" ,但是它输出"Good-bye.";
bye.", so obviously my function isn't doing what I want it to do. Why? And any suggestions on how to make
不按我的意愿去做,为什么?有什么建意按我的思法去做
it so?
firedraco (3536) Jan 5, 2011 at 5:36am
Your converter function is not actually changing the pointer you are passing.
你的转换函数指针其实并没有改变你过去了。
void converter(const char *aString)
{
const char *charPointer = "Hello!"; //make a local variable
charPointer = aString; //make it point to whatever aString is pointing at
}
Anyway, you can't actually change myString since it is a const char*. That means it is a pointer to some
constant (unchangeable) data, so if you try to modify it you will get an error.
不管怎样,你不能改变myString由于它是一个常量字符*。
,这意味着它是一个指向一些常数(不变的)数据,因此,如果你想要修改它,你将会得到一个错误。
Have you looked at the tutorial for pointers on this site? Try that if you haven't already.
如果你没有在网上看关于指针的教程?那么你尝试
PS:翻译还是一踏糊涂