如何将字符指针作为 C++ 函数的输出参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1217173/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to have a char pointer as an out parameter for C++ function
提问by Mark
I'm a newbie to C++. I'm trying to have a char pointer as an out parameter for a function. But the changes made in the function are not reflected in the main function. What am I doing wrong?
我是 C++ 的新手。我正在尝试将字符指针作为函数的输出参数。但是在函数中所做的更改并不会反映在主函数中。我究竟做错了什么?
void SetName( char *pszStr )
{
char* pTemp = new char[10];
strcpy(pTemp,"Mark");
pszStr = pTemp;
}
int _tmain(int argc, _TCHAR* argv[])
{
char* pszName = NULL;
SetName( pszName );
cout<<"Name - "<<*pszName<<endl;
delete pszName;
return 0;
}
回答by GManNickG
Your pointer is being copied onto the stack, and you're assigning the stack pointer. You need to pass a pointer-to-pointer if you want to change the pointer:
您的指针正在被复制到堆栈上,并且您正在分配堆栈指针。如果要更改指针,则需要传递一个指向指针的指针:
void SetName( char **pszStr )
{
char* pTemp = new char[10];
strcpy(pTemp,"Mark");
*pszStr = pTemp; // assign the address of the pointer to this char pointer
}
int _tmain(int argc, _TCHAR* argv[])
{
char* pszName = NULL;
SetName( &pszName ); // pass the address of this pointer so it can change
cout<<"Name - "<<*pszName<<endl;
delete pszName;
return 0;
}
That will solve your problem.
那将解决您的问题。
However, there are other problems here. Firstly, you are dereferencing your pointer before you print. This is incorrect, your pointer is a pointer to an array of characters, so you want to print out the entire array:
但是,这里还有其他问题。首先,您在打印之前取消引用您的指针。这是不正确的,您的指针是指向字符数组的指针,因此您要打印出整个数组:
cout<<"Name - "<<pszName<<endl;
What you have now will just print the first character. Also, you need to use delete []
to delete an array:
您现在拥有的只是打印第一个字符。此外,您需要使用delete []
来删除数组:
delete [] pszName;
Bigger problems, though, are in your design.
但是,更大的问题在于您的设计。
That code is C, not C++, and even then it's not standard. Firstly, the function you're looking for is main
:
该代码是 C,而不是 C++,即使这样它也不是标准的。首先,您正在寻找的功能是main
:
int main( int argc, char * argv[] )
Secondly, you should use referencesinstead of pointers:
其次,你应该使用引用而不是指针:
void SetName(char *& pszStr )
{
char* pTemp = new char[10];
strcpy(pTemp,"Mark");
pszStr = pTemp; // this works because pxzStr *is* the pointer in main
}
int main( int argc, char * argv[] )
{
char* pszName = NULL;
SetName( pszName ); // pass the pointer into the function, using a reference
cout<<"Name - "<<pszName<<endl;
delete pszName;
return 0;
}
Aside from that, it's usually better to just return things if you can:
除此之外,如果可以,通常最好只退货:
char *SetName(void)
{
char* pTemp = new char[10];
strcpy(pTemp,"Mark");
return pTemp;
}
int main( int argc, char * argv[] )
{
char* pszName = NULL;
pszName = SetName(); // assign the pointer
cout<<"Name - "<<pszName<<endl;
delete pszName;
return 0;
}
There is something that makes this all better. C++ has a string class:
有一些东西可以让这一切变得更好。C++ 有一个字符串类:
std::string SetName(void)
{
return "Mark";
}
int main( int argc, char * argv[] )
{
std::string name;
name = SetName(); // assign the pointer
cout<<"Name - "<< name<<endl;
// no need to manually delete
return 0;
}
If course this can all be simplified, if you want:
如果这一切都可以简化,如果你愿意:
#include <iostream>
#include <string>
std::string get_name(void)
{
return "Mark";
}
int main(void)
{
std::cout << "Name - " << get_name() << std::endl;
}
You should work on your formatting to make things more readable. Spaces inbetween your operators helps:
您应该处理格式以提高可读性。运算符之间的空格有助于:
cout<<"Name - "<<pszName<<endl;
cout << "Name - " << pszName << endl;
Just like spaces in between English words helps, sodoesspacesbetweenyouroperators. :)
就像英语单词之间的空格有帮助一样,sodoesspaces between youroperators。:)
回答by Kei
You can also use a reference to a pointer int this case. Also, you may want to be aware of 2 other bugs which are in the original code (see my comments in the code snippet).
在这种情况下,您还可以使用对指针 int 的引用。此外,您可能希望了解原始代码中的其他 2 个错误(请参阅我在代码片段中的评论)。
void SetName( char *& pszStr )
{
char* pTemp = new char[10];
strcpy(pTemp,"Mark");
pszStr = pTemp;
}
int _tmain(int argc, _TCHAR* argv[])
{
char* pszName = NULL;
SetName(pszName);
// Don't need '*' in front of pszName.
cout<< "Name - " << pszName << endl;
// Needs '[]' to delete an array.
delete[] pszName;
return 0;
}
回答by Brian R. Bondy
Since you tagged as C++, why not pass in a std::string reference and fill it?
既然你标记为 C++,为什么不传入一个 std::string 引用并填充它呢?
void GetName(std::string &strName)
{
strName = "Mark";
}
Or simply return an std::string:
或者简单地返回一个 std::string:
std::string GetName2()
{
return "Mark";
}
And call it like so
并像这样称呼它
std::string strName, strName2;
GetName(strName);
strName2 = GetName2();
assert(strName == "Mark");
assert(strName2 == "Mark");
//strName.c_str() returns the const char * pointer.
Then you don't have to worry about freeing any memory.
这样您就不必担心释放任何内存。
回答by Brian R. Bondy
What you are writing is not C++, but C code that uses new instead of malloc, and delete instead of free. If you really want to write C++ code, start again. Read a book like Accelerated C++, which will teach you modern idiomatic C++.
您正在编写的不是 C++,而是使用 new 代替 malloc 并使用 delete 代替 free 的 C 代码。如果您真的想编写 C++ 代码,请重新开始。阅读像Accelerated C++这样的书,它会教你现代惯用的 C++。