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 9031825
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T07:49:36+00:00 2026-06-16T07:49:36+00:00

Possible Duplicate: C++: Passing pointer variable to function Here is a simplified code snippet

  • 0

Possible Duplicate:
C++: Passing pointer variable to function

Here is a simplified code snippet which I think shows the problem at hand:

std::wstring *Variable3 = &SomeWStringObject;

int _tmain(int argc, _TCHAR* argv[])
{
    std::wstring *Variable1 = NULL;
    func(Variable1);
}

void func(std::wstring *Variable2)
{
    Variable2 = Variable3;
}

Now, in reality, func is a member of a class, and Variable3 is also a member of that same class. For this simpler example, let’s just assume that Variable3 is some sort of global variable.

Variable3 is a (global variable) pointer to a std::wstring object. I can see in the debugger than it is pointing to the correct string.

What I want to end up with is Variable1 pointing to the same std::wstring object as Variable3.

So I tried passing the address of pointer Variable1 into the function, which I hoped would then set the address Variable3 points to into Variable1.

But this isn’t working. It seems to be set OK, but then when the program leaves func, Variable1 is still a null pointer.

I have tried to be as clear as I can. I hope it is enough. Unfortunately, I cannot use the return value of func for this, as I actually have two other std::wstringstream objects to do the same thing to. Since all are having the same problem, I simplified it down to just one std::wstring object. I have tried lots of other different combinations of & and *, but none have worked.

Thank you very much for any help you can offer.

  • 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-06-16T07:49:37+00:00Added an answer on June 16, 2026 at 7:49 am

    At the moment you’re copying the value of Variable1 (which is the null pointer value) into func and then you’re modifying that copy. So the Variable1 outside func is not changed. The immediate solution for people who are a little pointer-prone is to add another level of indirection. Pass a pointer to your Variable1 and then modify it through that. Like so:

    func(&Variable1);
    
    void func(std::wstring** Variable2)
    {
      *Variable2 = Variable3;
    }
    

    However, this is not very good style. C++ has reference types that allow you to pass an object into a function without copying it, so that the object inside the function is the same as the object outside. We can use it like so:

    func(Variable1);
    
    void func(std::wstring*& Variable2)
    {
      Variable2 = Variable3;
    }
    

    Variable2 is a reference to a pointer (denoted by the &). It means that when you pass Variable1, it is not copied and Variable2 refers to precisely the same pointer. Modifying Variable2 will modify Variable1 too.

    The next question you should ask yourself is “Do I need a pointer to std::wstring in the first place?” I can’t answer that for you, but often the answer is no.

    And then the neeext question is why you are passing output through a parameter. This is a very C-ish thing to do (but even they wouldn’t do it in this situation). This is the reason we have return values. Ideally, your code would look like this:

    std::wstring Variable3 = SomeWStringObject;
    
    int _tmain(int argc, _TCHAR* argv[])
    {
      std::wstring Variable1 = func();
    }
    
    std::wstring func()
    {
      return Variable3;
    }
    

    Or potentially you will want to return a reference to Variable3, in whicih case your func return type should be std::wstring&.

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

Sidebar

Related Questions

Possible Duplicate: C#: Passing null to overloaded method - which method is called? Here
Possible Duplicate: Passing char pointer from C# to c++ function I have this type
Possible Duplicate: passing a pointer by reference in c++ I have a function that
Possible Duplicate: C/C++: Passing variable number of arguments around Imagine I have a function
Possible Duplicate: Difference between passing array and array pointer into function in C I
Possible Duplicate: passing in object by ref With the code below, the output would
Possible Duplicate: Passing javascript variable to PHP Hi I wonder if it's possible to
Possible Duplicate: C++ passing variables in from one Function to the Next. The Program
Possible Duplicate: Confusion over C++ pointer and reference topic Suppose I am passing int
Possible Duplicate: passing 2D array to function My question is related to passing an

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.