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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T07:23:39+00:00 2026-05-23T07:23:39+00:00

I want to pass an argument to a function, which takes a const char

  • 0

I want to pass an argument to a function, which takes a const char **

#include<iostream>

using namespace std;

void testFunc(const char **test){}

string testString = "This is a test string";

int main()
{
    const char *tempC = testString.c_str();

    testFunc(&tempC);

    return 0;
}

This code works fine, But I dont want to go through the temporary variable tempC. I want to pass testString.c_str() directly. Like the following,

int main()
{    
    testFunc(&testString.c_str());

    return 0;
}

But, it shows error,

 error C2102: '&' requires l-value

Is it possible to do it without using the temp variable.

  • 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-23T07:23:40+00:00Added an answer on May 23, 2026 at 7:23 am

    I want to pass testString.c_str() directly.

    You can’t. std::string::c_str() returns a const char *. In order to make a pointer to a string, you have to put it in a variable and take its address.

    That being said, I’m far more concerned about what function you’re trying to pass this to. In general, a function that takes a const char ** does so for two reasons:

    1: It takes an array of strings. You are passing a single string. Usually, C-style functions that take an array need a second parameter that says how many elements are in the array. I hope you’re putting a 1 in there.

    2: It is returning a string. In which case what you’re doing is not helpful at all. You should create a const char * as a variable, initialize it to NULL, and then pass a pointer to it as the parameter. It’s value will be filled in by the function.

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

Sidebar

Related Questions

A function takes a string as argument, example fn(string). I want to pass different
Here is my custom module using hook, Assume if I want to pass argument
I'm trying to figure out how to pass an argument to one function, which
how to pass multiple arguments in a single function in Objective-C? I want to
I want to call a PHP file but want to pass an argument to
I want to pass an enum value as command parameter in WPF, using something
I want to pass in the tType of a class to a function, and
Suppose I have a function which takes variadic arguments ( ... ) or a
like this question I want to pass a function with arguments. But I want
I have made one function named SetControls(control controlName) which only takes control as a

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.