1

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.

8
  • 2
    You can't. Why do you need a char** instead of a char* anyways? Commented Jun 20, 2011 at 4:48
  • Its another function which takes **const char ****, and I just need to send some data to it. I can't change the prototype. Commented Jun 20, 2011 at 4:51
  • @iamcreasy: what does it do though? Does it sometimes change the value of test? How and why? Without knowing that, we can tell you what's wrong with what you've done, but not how to do what you want to do.... Commented Jun 20, 2011 at 5:12
  • @Tony I have posted details on the first comment of @nicol-bolas 's answer. :) Commented Jun 20, 2011 at 5:14
  • 1
    @iamcreasy: I've voted to close this question as I think you'll find the one above covers the same topic for the same reason. Basically, the glShaderSource function you really want to call is expecting one or more strings of input, and accepts an array of pointers to those strings. You're better off writing code that reflects that expectation and lets you add more strings easily later, even if it means having a second line of code. Commented Jun 20, 2011 at 5:32

4 Answers 4

6

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Indeed. I am trying to write a function that takes a shader file as an input and turns it into a string, which I will be able to pass into the glShaderSource function. This tutorial uses const char* to show it.
@iamcreasy That's just how you have to do it. Put the return value of std::string::c_str() into a variable and take its address. Pass 1 to glShaderSource for the number of strings.
2

You're going to need two temp variables to call glShaderSource, so you might as well wrap them up in one function that takes the string directly.

#include <gl.h>
#include <string>

void setShaderFromString(std::string &instr, GLuint shader)
{
        const GLchar *str[1]; // room for one const GLchar *
        GLint   len[1];       // make this an array, for symmetry

        str[0] = instr.c_str();
        len[0] = instr.length();
        glShaderSource(shader, 1, str, len);

}

3 Comments

The length array is unnecessary. If you pass NULL for the last parameter, then it will simply assume the strings are NULL-terminated. Which is what c_str generates.
+1 Nice structured approach. Can take instr by const ref. @Nicol: sounds convenient, although trivially length() is O(1) and presumably avoids an O(N) operation within glShaderSource... ;-).
@Nicol - ok, not necessary, but we have the length, why not avoid another strlen.
0

Simply - no.

(filling up some space)

3 Comments

I don't believe this answer is useful. You don't provide a reason or a solution.
Well, it directly answered his question "is it possible" and didn't have the benefit of the additional context he had since provided in the comments of other answers.
It did, but it doesn't deserve to be upvoted. I don't think you can consider it as answer. Lets agree to disagree here ok?
0

You can also do type-casting:

int main()
{
    testFunc((const char **)&testString);
    return 0;
}

3 Comments

This assumes a STL implementation where the first member of testString happens to be a char*. Gnu libc is one. But it's not standard: static_cast<const char**>(&testString) is invalid.
it also assumes the pointed-to buffer is kept NUL terminated - overwhelmingly likely but not guaranteed by the C++03 Standard. Erik's concern is more practical: many reasons why an implementation might not have "the" char* there - small string optimisations, a pointer to a reference counted object, putting a size member first.... All up, it's really not a good idea :-/.
Can anyone give me a web reference where I can learn more about it. I want to know why this is not a good idea or / and nonstandard to use.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.