how to get char** length

im trying to get length from my bidimensional char array
 
char **storelist;

i dont know how to do it.
ive tried
1
2
3
4
strlen(storelist);//error cannot converto char** to char*
sizeof(storelist)/sizeof(char**);//ever return at 1
for(unsigned int i=0;storelist[i]!=NULL;i++)continue;//wrong result
for(unsigned int i=0;strlen(storelist[i]);i++)continue;//wrong result 


im using code::blocks and Borland compiler.

any solution to solve this issue?
this char **storelist; is not a definition of two-dimensional array. I suspect that it is a pointer to an arrray of pointers. Then you need know what is the size of the array of pointers.
Vlad is correct, it is a pointer, which when initialized, becomes an array of pointers. While the pointers in the array are sequential in memory, their value (the address they point to) is not.

If Line 3 is giving you the wrong answer, then I suspect you want more than the amount of char* that you have created. I assume you want the length of all of the c-strings combined.

If this is the case, then you need a little more to that for loop:
1
2
3
int total = 0;
for(unsigned int i=0;storelist[i]!=NULL;i++)
  total += strlen(storelist[i]);

hm
i want know how much strings the pointer have
eg:
 
char *storelist[]={"Grass","Forniture","Tools"};

this is a pre-defined array,then i know the length is 3
but when there are so much values kind ~200 values in the array pointer, how to get the number of char * of easy way?

sorry for my bad english, im still learning....
Last edited on
This is a very different question.
1
2
char **storelist; // this is a pointer
const char *storelist[]={"Grass","Forniture","Tools"}; // this is an array (note 'char *' is an error here 



If you have a pointer, there is nothing you can do.

If you have an array, you can find its size by dividing the size of the array by the size of an element
1
2
3
4
5
6
7
#include <iostream>

int main() {
    const char* storelist[] = {"Grass", "Furniture", "Tools"};
    int storelist_sz = sizeof storelist / sizeof storelist[0];
    std::cout << "number of stores: " << storelist_sz << '\n';
}


If you have a vector or another usual C++ container, you can call the size() member function:

1
2
3
4
5
6
7
#include <iostream>
#include <vector>
#include <string>
int main() {
    std::vector<std::string> storelist = {"Grass", "Furniture", "Tools"};
    std::cout << "number of stores: " << storelist.size() << '\n';
}
Topic archived. No new replies allowed.