Array of char*
-
Hello all... how do I create array of
char*
. At the moment I have hardcoded the number of instances (large enough to hold tokens) but this should be generic. Exampleint temp_array[2] = {0};
char* token1;
char* token2;As you can see, I might not know how many tokens I need at run time. I want something like this
char* [] tokens;
How could I fix this? Thanks
-
Hello all... how do I create array of
char*
. At the moment I have hardcoded the number of instances (large enough to hold tokens) but this should be generic. Exampleint temp_array[2] = {0};
char* token1;
char* token2;As you can see, I might not know how many tokens I need at run time. I want something like this
char* [] tokens;
How could I fix this? Thanks
Create a variable for the array that is initially empty and a variable holding the size:
char **tokens = NULL;
unsigned tokenSize = 0;Once you know the required number of items, allocate the array. How to do that depends if you are using C or C++:
tokenSize = items_to_store_in_array;
// C++
tokens = new char *[tokenSize];/* C */
tokens = (char **)malloc(sizeof(char*) * tokenSize);If you need to change the size use
realloc
with C. With C++ you should then avoid arrays and use a container like vector - C++ Reference[^] instead. -
Create a variable for the array that is initially empty and a variable holding the size:
char **tokens = NULL;
unsigned tokenSize = 0;Once you know the required number of items, allocate the array. How to do that depends if you are using C or C++:
tokenSize = items_to_store_in_array;
// C++
tokens = new char *[tokenSize];/* C */
tokens = (char **)malloc(sizeof(char*) * tokenSize);If you need to change the size use
realloc
with C. With C++ you should then avoid arrays and use a container like vector - C++ Reference[^] instead.Jochen Arndt wrote:
How to do that depends if you are using C or C++:
Well I should have mentioned that earlier..... I am using C. Can you please edit your question (or append the code for C)
-
Jochen Arndt wrote:
How to do that depends if you are using C or C++:
Well I should have mentioned that earlier..... I am using C. Can you please edit your question (or append the code for C)
The C code is there (the
malloc
call). Use something like this (for two tokens):tokenSize = 2;
tokens = (char **)malloc(sizeof(char*) * tokenSize);
tokens[0] = token1;
tokens[1] = token2;/* Use tokens array here */
/* Release memory and reset variables when not used anymore */
free(tokens);
tokens = NULL;
tokenSize = 0; -
Hello all... how do I create array of
char*
. At the moment I have hardcoded the number of instances (large enough to hold tokens) but this should be generic. Exampleint temp_array[2] = {0};
char* token1;
char* token2;As you can see, I might not know how many tokens I need at run time. I want something like this
char* [] tokens;
How could I fix this? Thanks
std::vactor m_myArrayOfString ; m_myArrayOfString.push_back(new char[17]); ... ... in the end loop through vector and delete them one by one for(auto ptr : m_myArrayOfString) delete [] ptr ; m_myArrayOfString.clear();
huzifa
-
Jochen Arndt wrote:
How to do that depends if you are using C or C++:
Well I should have mentioned that earlier..... I am using C. Can you please edit your question (or append the code for C)
Only addition to Jochens answer is to remove confusion to you typedef the char* pointer
ptrCount = 2;
typedef char* CHARPTR;
CHARPTR* charPtrArray = (CHARPTR*)malloc(sizeof(CHARPTR)*ptrcount);Now it looks like any other array The confusion exists because char** can be a couple of things in C you can't distinguish them without looking at the initialization code 1.) A pointer to a char* (always true) 2.) An array of char* (if initialized as such) 3.) A multidimensional char array (if initialized as such) Number 3 in particular usually causes mass confusion with students learning C. The typedef doesn't stop you assuming the wrong thing but it does give you language cues to what it is and is easier to read.
In vino veritas
-
Hello all... how do I create array of
char*
. At the moment I have hardcoded the number of instances (large enough to hold tokens) but this should be generic. Exampleint temp_array[2] = {0};
char* token1;
char* token2;As you can see, I might not know how many tokens I need at run time. I want something like this
char* [] tokens;
How could I fix this? Thanks
Think of an array of chars as a 2d map. So along the bottom the x axis can be considdered as an array of char pointers, each one pointing to a vertical array of chars going up in the y direction. So point 0,0 is a char** You can of course extend this to a 3 d map, with char*** Any point on the 2d map can be referenced by data[x][y] SO first you alloc how many you need in the x direction: char** data = alloc(xMax) Now each vertical collum can be alloced as data[0] = alloc(y1Max) data[1] = alloc(y2Max) And so on.