如何用dymanically填充char *指针数组? [英] How to populate an array of char* pointer dymanically?
问题描述
在我的代码中,我有一个char *指针数组,其中填充了
静态:
void function1(){
char * ppsz_argv2 [] = {" abc" ,
" def",
" dummy"};
// ...
}
但是我怎样才能动态填充它?
void function1(string& str1,string& str2,string& str3){<
char * ppsz_arg2 [] =新char [3]; //我怎么能在这里为
char *数组分配内存?
ppsz_arg2 [0] = str1.c_str();
ppsz_arg2 [1] = str2.c_str();
ppsz_arg2 [2] = str3.c_str();
感谢您的帮助。 />
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.
推荐答案
si *************** @ gmail.com 写道:
在我的代码中,我有填充的char *指针数组
静态:
void function1(){
char * ppsz_argv2 [] = {" abc" ,
" def",
" dummy"};
// ...
}
但是我怎样才能动态填充它?
void function1(string& str1,string& str2,string& str3){<
char * ppsz_arg2 [] =新char [3]; //我怎么能在这里为
char *数组分配内存?
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?
这里你分配一个3个字符的数组。你想要改为
为const chars分配一个3 *指针*的数组。
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();
ppsz_arg2[0] = str1.c_str();
ppsz_arg2[1] = str2.c_str();
ppsz_arg2[2] = str3.c_str();
目前为止最好的方法是使用std :: vector。
std :: vector< const char * > vec(3);
vec [0] = str1.c_str();
vec [1] = str2.c_str();
vec [2] = str3.c_str();
现在你没有probs会释放后记忆。
BTW - 任何修改str1,str2和str3的东西都会使
指针无效,因此请确保它们永远不会被触及
你是使用vec。
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.
6月10日下午7:03,Gianni Mariani< gi3nos ... @ mariani.wswrote:
On Jun 10, 7:03 pm, Gianni Mariani <gi3nos...@mariani.wswrote:
silverburgh.me ... @ gmail.com写道:
silverburgh.me...@gmail.com wrote:
在我的代码中,我有一个char *指针数组,填充<静态
:
In my code, I have an array of char* pointer which is populated
statically:
void function1(){
char * ppsz_argv2 [] = {" ; ABC" ,
" def",
" dummy"};
// ...
}
void function1() {
char *ppsz_argv2[] = { "abc" ,
"def",
"dummy"};
//...
}
但我怎样才能动态填充它?
But how can I populated it dynamically?
void function1(string& str1,string& str2,string& str3){
void function1(string& str1, string& str2, string& str3) {
char * ppsz_arg2 [] = new char [3]; //我怎么能在这里为
char *数组分配内存?
char* ppsz_arg2[] = new char[3]; // how can I allocate memory for the
array of char* here?
这里你分配一个由3个字符组成的数组。你想要改为
为const chars分配一个3 *指针*的数组。
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();
ppsz_arg2[0] = str1.c_str();
ppsz_arg2[1] = str2.c_str();
ppsz_arg2[2] = str3.c_str();
目前为止最好的方法是使用std :: vector。
std :: vector< const char * vec(3);
vec [0] = str1.c_str();
vec [1] = str2.c_str();
vec [2] = str3.c_str();
现在你没有probs会释放后记忆。
BTW - 任何修改str1,str2和str3的东西都会使
指针无效,因此请确保它们永远不会被触及
你是使用vec。
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.
谢谢。但是我需要调用一个函数,稍后会使用'char * ppsz_arg2 []''
。
如何从''std转换: :vector< const char *>''to''char *
ppsz_arg2 []''?
感谢您的任何其他指示。
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.
< si *************** @ gmail.comwrote in message ...
<si***************@gmail.comwrote in message ...
6月10日晚上7:03,Gianni Mariani< gi3nos ... @ mariani.wswrote:
On Jun 10, 7:03 pm, Gianni Mariani <gi3nos...@mariani.wswrote:
silverburgh.me ... @ gmail.com写道:
silverburgh.me...@gmail.com wrote:
在我的代码中,我有一个char *指针数组,其中填充了
静态:
void function1(){
char * ppsz_argv2 [] = {" abc"," def"," dummy"};
// ...
}
In my code, I have an array of char* pointer which is populated
statically:
void function1() {
char *ppsz_argv2[] = { "abc", "def", "dummy"};
//...
}
目前为止最好的方法是使用std :: vector。
std :: vector< const char * vec(3);
vec [0] = str1.c_str();
vec [1] = str2.c_str();
vec [2] = str3 .c_str();
现在你没有probs会释放后记忆。
BTW - 任何修改str1,str2和str3的东西都会使向量中放置的
指针无效,因此确保它们永远不会被触摸,而
你正在使用vec。
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.
谢谢。但是我需要调用一个函数,后来需要''char * ppsz_arg2 []''
。
如何从''std :: vector< const转换char *>''to''char *
ppsz_arg2 []''?
感谢您的任何其他指示。
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);
// ....填充向量
CharFunc(& Vppsz.at(0),Vppsz.size());
}
或者,甚至更好,改为:
void CharFunc(std :: vector< char const * const& ppsz){
返回;
}
{
std: :矢量< char const * Vppsz(3);
// ....填充矢量
CharFunc(Vppsz);
}
最好的:
void CharFunc(std :: vector< std :: string / * const * /& ppsz){
返回;
}
{
std :: vector< std :: string Vppsz(3);
// ....填充矢量
CharFunc(Vppsz);
}
-
Bob R
POVrookie
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
这篇关于如何用dymanically填充char *指针数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!