Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to have a char pointer as an out parameter for C++ function

Tags:

c++

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;
}
like image 362
Mark Avatar asked Aug 01 '09 18:08

Mark


People also ask

How do you pass a char pointer to a function?

Here in main function, we call mystrlen() and pass a reference address of str. In function mystrlen(), we modify/increase(str++) the string pointer till '\0'. In main() we print the str after function call mystrlen.

What is output parameter in C?

An out-parameter represents information that is passed from the function back to its caller. The function accomplishes that by storing a value into that parameter. Use call by reference or call by pointer for an out-parameter.

Can a char pointer point to a string?

We can create a character pointer to string in C that points to the starting address of the character array. This pointer will point to the starting address of the string, that is the first character of the string, and we can dereference the pointer to access the value of the string.

Can a char pointer point to a string C++?

Is it possible to point a string with a character pointer? You can use std::string::c_str that "Returns a pointer to an array that contains a null-terminated sequence of characters (i.e., a C-string) representing the current value of the string object."


2 Answers

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 [] 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:

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

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:

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. :)

like image 98
GManNickG Avatar answered Oct 19 '22 10:10

GManNickG


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).

 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;
 }
like image 33
Kei Avatar answered Oct 19 '22 09:10

Kei