How to populate an array of char* pointer dymanically? - c++
This is a discussion on How to populate an array of char* pointer dymanically? - c++ ; In my code, I have an array of char* pointer which is populated
statically:
void function1() {
char *ppsz_argv2[] = { "abc" ,
"def",
"dummy"};
//...
}
But how can I populated it dynamically?
void function1(string& str1, string& str2, string& ...
-
How to populate an array of char* pointer dymanically?
In my code, I have an array of char* pointer which is populated
statically:
void function1() {
char *ppsz_argv2[] = { "abc" ,
"def",
"dummy"};
//...
}
But how can I populated it dynamically?
void function1(string& str1, string& str2, string& str3) {
char* ppsz_arg2[] = new char[3]; // how can I allocate memory for the
array of char* here?
ppsz_arg2[0] = str1.c_str();
ppsz_arg2[1] = str2.c_str();
ppsz_arg2[2] = str3.c_str();
Thank you for any help.
-
Re: How to populate an array of char* pointer dymanically?
silverburgh.meryl@ wrote:
> In my code, I have an array of char* pointer which is populated
> statically:
>
> void function1() {
> char *ppsz_argv2[] = { "abc" ,
> "def",
> "dummy"};
> //...
> }
>
> But how can I populated it dynamically?
>
> void function1(string& str1, string& str2, string& str3) {
>
> char* ppsz_arg2[] = new char[3]; // how can I allocate memory for the
> array of char* here?
Here you're allocating an array of 3 chars. You want to instead
allocate an array of 3 *pointers* to const chars.
> ppsz_arg2[0] = str1.c_str();
> ppsz_arg2[1] = str2.c_str();
> ppsz_arg2[2] = str3.c_str();
Best way by far to do this is use std::vector.
std::vector<const char *> vec( 3 );
vec[0] = str1.c_str();
vec[1] = str2.c_str();
vec[2] = str3.c_str();
Now you don't have probs will freeing the memory afterwoods.
BTW - anything that modifies str1, str2 and str3 will invalidate the
pointers placed in the vector so make sure they are never touched while
you're using vec.
-
Re: How to populate an array of char* pointer dymanically?
On Jun 10, 7:03 pm, Gianni Mariani <gi3nos...@mariani.ws> wrote:
> silverburgh.me...@ wrote:
> > In my code, I have an array of char* pointer which is populated
> > statically:
>
> > void function1() {
> > char *ppsz_argv2[] = { "abc" ,
> > "def",
> > "dummy"};
> > //...
> > }
>
> > But how can I populated it dynamically?
>
> > void function1(string& str1, string& str2, string& str3) {
>
> > char* ppsz_arg2[] = new char[3]; // how can I allocate memory for the
> > array of char* here?
>
> Here you're allocating an array of 3 chars. You want to instead
> allocate an array of 3 *pointers* to const chars.
>
> > ppsz_arg2[0] = str1.c_str();
> > ppsz_arg2[1] = str2.c_str();
> > ppsz_arg2[2] = str3.c_str();
>
> Best way by far to do this is use std::vector.
>
> std::vector<const char *> vec( 3 );
>
> vec[0] = str1.c_str();
> vec[1] = str2.c_str();
> vec[2] = str3.c_str();
>
> Now you don't have probs will freeing the memory afterwoods.
>
> BTW - anything that modifies str1, str2 and str3 will invalidate the
> pointers placed in the vector so make sure they are never touched while
> you're using vec.
Thanks. But I need to call a function which takes 'char* ppsz_arg2[]'
later on.
How can I convert from 'std::vector<const char *>' to 'char*
ppsz_arg2[]'?
Thanks for any other pointers.
-
Re: How to populate an array of char* pointer dymanically?
<silverburgh.meryl@> wrote in message ...
> On Jun 10, 7:03 pm, Gianni Mariani <gi3nos...@mariani.ws> wrote:
> > silverburgh.me...@ wrote:
> > > In my code, I have an array of char* pointer which is populated
> > > statically:
> > > void function1() {
> > > char *ppsz_argv2[] = { "abc", "def", "dummy"};
> > > //...
> > > }
> >
> > Best way by far to do this is use std::vector.
> >
> > std::vector<const char *> vec( 3 );
> > vec[0] = str1.c_str();
> > vec[1] = str2.c_str();
> > vec[2] = str3.c_str();
> >
> > Now you don't have probs will freeing the memory afterwoods.
> > BTW - anything that modifies str1, str2 and str3 will invalidate the
> > pointers placed in the vector so make sure they are never touched while
> > you're using vec.
>
> Thanks. But I need to call a function which takes 'char* ppsz_arg2[]'
> later on.
> How can I convert from 'std::vector<const char *>' to 'char*
> ppsz_arg2[]'?
> Thanks for any other pointers.
>
void CharFunc( char const *arg[], std::size_t size ){
return;
}
{
std::vector< char const * > Vppsz(3);
// .... fill vector
CharFunc( &Vppsz.at(0), Vppsz.size() );
}
Or, even better, change to:
void CharFunc( std::vector< char const *> const &ppsz ){
return;
}
{
std::vector< char const * > Vppsz(3);
// .... fill vector
CharFunc( Vppsz );
}
Best yet:
void CharFunc( std::vector< std::string > /*const*/ &ppsz ){
return;
}
{
std::vector< std::string > Vppsz(3);
// .... fill vector
CharFunc( Vppsz );
}
--
Bob R
POVrookie
-
Re: How to populate an array of char* pointer dymanically?
On Jun 10, 10:01 pm, "BobR" <removeBadB...@worldnet.att.net> wrote:
> <silverburgh.me...@> wrote in message ...
> > On Jun 10, 7:03 pm, Gianni Mariani <gi3nos...@mariani.ws> wrote:
> > > silverburgh.me...@ wrote:
> > > > In my code, I have an array of char* pointer which is populated
> > > > statically:
> > > > void function1() {
> > > > char *ppsz_argv2[] = { "abc", "def", "dummy"};
> > > > //...
> > > > }
>
> > > Best way by far to do this is use std::vector.
>
> > > std::vector<const char *> vec( 3 );
> > > vec[0] = str1.c_str();
> > > vec[1] = str2.c_str();
> > > vec[2] = str3.c_str();
>
> > > Now you don't have probs will freeing the memory afterwoods.
> > > BTW - anything that modifies str1, str2 and str3 will invalidate the
> > > pointers placed in the vector so make sure they are never touched while
> > > you're using vec.
>
> > Thanks. But I need to call a function which takes 'char* ppsz_arg2[]'
> > later on.
> > How can I convert from 'std::vector<const char *>' to 'char*
> > ppsz_arg2[]'?
> > Thanks for any other pointers.
>
> void CharFunc( char const *arg[], std::size_t size ){
> return;
> }
>
> {
> std::vector< char const * > Vppsz(3);
> // .... fill vector
> CharFunc( &Vppsz.at(0), Vppsz.size() );
>
> }
>
> Or, even better, change to:
>
> void CharFunc( std::vector< char const *> const &ppsz ){
> return;
> }
>
> {
> std::vector< char const * > Vppsz(3);
> // .... fill vector
> CharFunc( Vppsz );
>
> }
>
> Best yet:
>
> void CharFunc( std::vector< std::string > /*const*/ &ppsz ){
> return;
> }
>
> {
> std::vector< std::string > Vppsz(3);
> // .... fill vector
> CharFunc( Vppsz );
>
> }
>
> --
> Bob R
> POVrookie
Thanks. I get garabage when I try to print out arg[] in my CharFunc():
void CharFunc( char const *arg[], std::size_t size ){
// when I print out the content of arg[] here, I get garbage for
arg[1] (just arg[1])
return;
}
void anotherFun(string& str){
{
std:
stringstream apath;
apath << "http://";
apath << str;
apath << '\0';
std::vector< const * > Vppsz(3);
vec[0] = "abc";
vec[1] = (char*)apath.str().c_str();
vec[2] = "hello";
CharFunc( &Vppsz.at(0), Vppsz.size() );
}
Can you please tell me why is that? Thank you
-
Re: How to populate an array of char* pointer dymanically?
"Silverburgh Meryl" wrote:
> void function1(string& str1, string& str2, string& str3)
> {
> // how can I allocate memory for the array of char* here?
> char* ppsz_arg2[] = new char[3];
> ...
> }
Why do you need dynamic allocation if the number of elements
is always three?
If you're *sure* you need dynamic allocation for the array,
then allocate more array space than you think you'll need.
(If you run out of space, you'll have to re-allocate, which
will get very messy.)
Your syntax has multiple errors, though. It should look
more like this:
// new-array-test.cpp
#include <iostream>
#include <string>
#include <cstring>
#include <new>
void
function1
(
std::string const & str1,
std::string const & str2,
std::string const & str3
)
{
char ** ppsz_arg2 = new char* [50];
ppsz_arg2[0] = new char [str1.size() + 1];
std::strcpy(ppsz_arg2[0], str1.c_str());
ppsz_arg2[1] = new char [str2.size() + 1];
std::strcpy(ppsz_arg2[1], str2.c_str());
ppsz_arg2[2] = new char [str3.size() + 1];
std::strcpy(ppsz_arg2[2], str3.c_str());
std::cout << "First string is: " << ppsz_arg2[0] << std::endl;
std::cout << "Second string is: " << ppsz_arg2[1] << std::endl;
std::cout << "Third string is: " << ppsz_arg2[2] << std::endl;
return;
}
int main (int, char * Luthien[])
{
function1(Luthien[1], Luthien[2], Luthien[3]);
return 0;
}
If I then type, at the command prompt:
new-array-test Able Baker Charly
It prints:
First string is: Able
Second string is: Baker
Third string is: Charly
But if you're only going to have 3 elements, you should
use a static array.
Replace this line:
char ** ppsz_arg2 = new char* [50];
with this line:
char *ppsz_arg2[3];
(Better yet, dump all usage of C-style char* and use
std::string instead, throughout your program. It's
sooooooo much easier.)
--
Cheers,
Robbie Hatley
lonewolf aatt well dott com
triple-dubya dott tustinfreezone dott org
-
Re: How to populate an array of char* pointer dymanically?
silverburgh.meryl@ wrote:
....
> Can you please tell me why is that? Thank you
Please post exactly the code you're using to demonstrate the errors
because the code you posted in your last post won't compile.
Also, make sure you don't touch the string after placing it in the
vector and the vector has yet to be destroyed.
-
Re: How to populate an array of char* pointer dymanically?
<silverburgh.meryl@> wrote in message ...
> >
> Thanks. I get garabage when I try to print out arg[] in my CharFunc():
>
> void CharFunc( char const *arg[], std::size_t size ){
> // when I print out the content of arg[] here, I get garbage for
> arg[1] (just arg[1])
> return;
> }
>
> void anotherFun(string& str){ {
> std:
stringstream apath;
> apath << "http://";
> apath << str;
> apath << '\0';
> std::vector< const * > Vppsz(3);
> vec[0] = "abc";
> vec[1] = (char*)apath.str().c_str();
> vec[2] = "hello";
> CharFunc( &Vppsz.at(0), Vppsz.size() );
> }
>
> Can you please tell me why is that? Thank you
You need to put something *in* the vector!!!
#include <iostream>
#include <vector>
#include <ostream>
void CharFunc( char const *arg[], std::size_t size,
std:
stream &out ){
for( std::size_t i(0); i < size; ++i ){
out<<arg[i]<<std::endl;
}
return;
}
int main(){ using std::cout; // for NG post
std::vector< char const *> Vppsz(3);
Vppsz.at(0) = "abc"; // NOT vec[]
Vppsz.at(1) = "def";
Vppsz.at(2) = "dummy";
CharFunc( &Vppsz.at(0), Vppsz.size(), cout );
return 0;
} // main()
Note how I have posted a *complete*, compilable program, which demonstrates
a problem (if there were one, in which case I would also post the first 3 or
4 errors). That's how you should post in this NG.
When you post little bits of here and there code, it's hard to determine
exactly what errors you are getting (and/or why).
--
Bob R
POVrookie
-
-
Re: How to populate an array of char* pointer dymanically?
On Jun 11, 1:55 pm, "BobR" <removeBadB...@worldnet.att.net> wrote:
> ... additional ...
>
> > <silverburgh.me...@> wrote in message ...
> > > void anotherFun(string& str){ {
> > > std:
stringstream apath;
> > > apath << "http://";
> > > apath << str;
> > > apath << '\0';
>
> Learn to use std::string.....
>
> std::string apath( "http://" );
> apath += str;
> // apath += '\0'; // not needed, use 'apath.c_str()'
>
> Now look how easy it is with std:string...
>
> > #include <iostream>
> > #include <vector>
> > #include <ostream>
>
> #include <string>
>
> void FillVector( std::vector< std::string > &vec){ // non-const here
> vec.push_back( "abc" );
> vec.push_back( "def" );
> vec.push_back( "dummy" );
> return;
> } // FillVector(vector<string>&)
>
> void PrintVector( std::vector< std::string > const &vec,
> std:
stream &out ){
> for( std::size_t i(0); i < vec.size(); ++i ){ // vector has the size
> out<<vec.at( i )<<std::endl;
> // out<<vec.[ i ]<<std::endl; // ok here, index is safe
> } // for(i)
> return;
> } // PrintVector(vector<string>const&,ostream &)
>
> > int main(){ using std::cout; // for NG post
>
> std::vector< std::string > MyVec;
> FillVector( MyVec );
> PrintVector( MyVec, cout );
>
> > return 0;
> > } // main()
>
> --
> Bob R
> POVrookie
Bob,
Thank you very much for your help. i have one more question:
void FillVector( std::vector< std::string > &vec){
vec.push_back( "abc" );
vec.push_back( "def" );
vec.push_back( "dummy" );
}
Where does these string "abc", "def", "dummy" allocated? Stack or
heap?
And do i need to call destructor for those strings?
Thank you.
Similar Threads
-
By Application Development in forum DOTNET
Replies: 3
Last Post: 11-09-2007, 03:01 PM
-
By Application Development in forum labview
Replies: 0
Last Post: 10-04-2007, 01:40 PM
-
By Application Development in forum C
Replies: 8
Last Post: 09-27-2007, 06:08 AM
-
By Application Development in forum c++
Replies: 5
Last Post: 06-10-2007, 05:37 PM
-
By Application Development in forum c++
Replies: 8
Last Post: 02-27-2007, 07:35 PM