R
rob.kirkpatrick
Hello
I need to populate an array of char arrays at run-time. A very
simplifed version of the code is below.
char ** list should contain cnt char arrays. The values of char ** list
are set by the function foo(). A pointer to char ** list is passed to
foo() as an argument.
The problem is that when foo() returns, char ** list contains rubbish
and does not always contain cnt char arrays. Can anyone tell me what
I'm doing wrong? Note that I don't want to use the vector or string for
efficiency. Note that the code that get the time has nothing to do with
the real programme. It's just a simplification for the purpose of this
posting.
(I have noticed that if the values are constant char arrays there is no
problem)
//*********************************************************************************
#include <iostream.h>
#include <time.h>
#include <string.h>
#include <stdlib.h>
void foo(char *** array, int cnt){
char buf[256];
for(int i=0; i<cnt; i++){
long t = time(NULL); // just for demonstration
itoa(t, buf, 10);
(*array) = buf;
cout << i << " " << (*array) << '\n';
}
}
int main(int argc, char ** argv){
int cnt = 10;
char ** list = new char*[10];
foo(&list, cnt);
for(int i=0; i<cnt; i++){
cout << i << " " << list << "\n";
}
delete [] list;
return 0;
}
//*********************************************************************************
Thanks!!!!!
I need to populate an array of char arrays at run-time. A very
simplifed version of the code is below.
char ** list should contain cnt char arrays. The values of char ** list
are set by the function foo(). A pointer to char ** list is passed to
foo() as an argument.
The problem is that when foo() returns, char ** list contains rubbish
and does not always contain cnt char arrays. Can anyone tell me what
I'm doing wrong? Note that I don't want to use the vector or string for
efficiency. Note that the code that get the time has nothing to do with
the real programme. It's just a simplification for the purpose of this
posting.
(I have noticed that if the values are constant char arrays there is no
problem)
//*********************************************************************************
#include <iostream.h>
#include <time.h>
#include <string.h>
#include <stdlib.h>
void foo(char *** array, int cnt){
char buf[256];
for(int i=0; i<cnt; i++){
long t = time(NULL); // just for demonstration
itoa(t, buf, 10);
(*array) = buf;
cout << i << " " << (*array) << '\n';
}
}
int main(int argc, char ** argv){
int cnt = 10;
char ** list = new char*[10];
foo(&list, cnt);
for(int i=0; i<cnt; i++){
cout << i << " " << list << "\n";
}
delete [] list;
return 0;
}
//*********************************************************************************
Thanks!!!!!