如何使一个字符指针作为一个out参数为C ++函数 [英] How to have a char pointer as an out parameter for C++ function

查看:109
本文介绍了如何使一个字符指针作为一个out参数为C ++函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是C ++的新手。我试图有一个char指针作为一个函数的输出参数。但是在函数中所做的更改不会反映在主函数中。我做错了什么?

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?

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;
}


推荐答案

堆栈,并且你正在分配堆栈指针。如果要更改指针,则需要传递指针:

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;
}

这将解决你的问题。

但是,这里还有其他问题。首先,您在打印之前取消引用指针。这是不正确的,你的指针是一个指向字符数组的指针,所以你要打印出整个数组:

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;

现在你只需打印第一个字符。 另外,您需要使用 delete [] 删除数组:

What you have now will just print the first character. Also, you need to use delete [] to delete an array:

delete [] pszName;






但是更大的问题在您的设计中。


Bigger problems, though, are in your design.

代码是C,而不是C ++,甚至不是标准。首先,您要查找的函数是 main

That code is C, not C++, and even then it's not standard. Firstly, the function you're looking for is main:

int main( int argc, char * argv[] )

其次,你应该使用参考,而不是指针:

Secondly, you should use references instead 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;
}

除此之外,通常最好只是返回一些东西, / p>

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;
}

有一些东西让这一切都更好。 C ++有一个字符串类

There is something that makes this all better. C++ has a string class:

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;

就像英语单词之间的空格有帮助,sodoesspaces betweenyouroperators。 :)

Just like spaces in between English words helps, sodoesspacesbetweenyouroperators. :)

这篇关于如何使一个字符指针作为一个out参数为C ++函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆