Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 306695
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T07:29:26+00:00 2026-05-12T07:29:26+00:00

I’m a newbie to C++. I’m trying to have a char pointer as an

  • 0

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;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T07:29:26+00:00Added an answer on May 12, 2026 at 7:29 am

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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to loop through a bunch of documents I have to put
I have a French site that I want to parse, but am running into
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I have a bunch of posts stored in text files formatted in yaml/textile (from
I am trying to understand how to use SyndicationItem to display feed which is
this is what i have right now Drawing an RSS feed into the php,
Seemingly simple, but I cannot find anything relevant on the web. What is the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.