I have an opengl function that requires a const char**. So essentially a vector of strings. I was wondering if this could be done using the C++ Standard Library without making a vector of const char*
which would demand heap allocation.
Asked
Active
Viewed 148 times
-2

Lightness Races in Orbit
- 378,754
- 76
- 643
- 1,055

jmasterx
- 52,639
- 96
- 311
- 557
-
2The title says `char **` but the body says `const char*` and gets more confusing from there (why are you trying to avoid heap allocation?). Please clarify, preferably with code. – Nicholas Knight May 29 '11 at 23:35
-
I need to read strings from a file and I want to add and remove strings with ease. – jmasterx May 30 '11 at 00:01
1 Answers
5
If you're wanting to avoid heap allocation:
const char* arrayOfStrings[] = {
"first string",
"second string",
"third string"
};
And you can use arrayOfStrings
as a const char**
.
If you have a vector of const char*
s you can just get the address of the first element to obtain a const char**
:
&vec[0];
However, what do you mean when you say "using [c++ standard library]"? You say you want to "use the [C++ standard libary] without making a vector of const char*
". That doesn't make much sense.
You don't need the standard library for this, please clarify your question if my answer isn't sufficient.

Seth Carnegie
- 73,875
- 22
- 181
- 249
-
And, presumably he's referring to the C++ Standard Library, not the STL. – Lightness Races in Orbit May 29 '11 at 23:45
-
-
1@Seth: [The STL is *not* the C++ Standard Library.](http://stackoverflow.com/questions/5205491/whats-this-stl-vs-c-standard-library-fight-all-about/5205571#5205571) – Lightness Races in Orbit May 29 '11 at 23:49
-
@Tomalak Obviously, and no one here thinks it is, but why was your comment enclosed in
tags – Seth Carnegie May 29 '11 at 23:50 -
1@Seth: Many people think that it is. You yourself referred to it in your answer, despite the fact that pretty much nobody uses the STL any more, and `std::` certainly has nothing to do with it. However, this discussion often becomes a heated debate, which is why I decided in advance to declare a slight tongue-in-cheek approach to my original comment (in an attempt to avoid a debate, whilst still satisfying my OCD urges to make the correction :P). – Lightness Races in Orbit May 29 '11 at 23:52
-
1@Tomalak Ohhhhhhh, I see what you mean. In that case, I was mistaken as I thought STL was a synonym for the thing that contains `vector` and all those headers and stuff. I'll _attempt_ to fix my answer. Thanks, I had not known that before. – Seth Carnegie May 29 '11 at 23:55
-
1@Seth: Disclaimer: Not everybody agrees with my viewpoint. As much as I wish I could claim 100% authority, "mistaken" might be a little strong a word here. (And, FWIW, the STL _does_ have a `vector`... but it does not have `std::vector` - which comes with your toolchain.) – Lightness Races in Orbit May 29 '11 at 23:58
-
@Tomalak I know this is unrelated but I don't know where else to ask. I read this sentence in your profile thing: `and there's no such thing as "heap" or "stack" in the context in which you're trying to use it`. I _think_ I know what the stack and heap (free store) are, but that sentence makes me less confident, considering what I just learned about the STL. So what does that sentence in your profile mean? – Seth Carnegie May 30 '11 at 00:27
-
@Seth: When people say "stack" or "heap", they usually mean "automatic storage duration" or "dynamic storage duration" respectively. Oftentimes a heap structure (as you say, "free store" is better) is not even involved, and "stack objects" if members of a dynamically allocated object are not on your callstack. – Lightness Races in Orbit May 30 '11 at 00:34