-2

As Question says is there a way to read how many elements are in the char** array?

In following code print function, should find count of elements in charpp array without reading sizeofArray variable.

#include <iostream>
using  namespace std;

char** charpp;

void print(char** charpp){
    cout << "Size: " << sizeof(&charpp) << "\r\n";
    for(int i=0;i< sizeofArray;i++){
        if (charpp[i]!=NULL)
            cout << i << " : " << charpp[i] << "\r\n";
    }
}

void main() {
    int sizeofArray = 27;

    charpp = new char*[sizeofArray];
    for(int i = 0; i < sizeofArray; i++) {
      charpp[i] = NULL;
    }

    charpp[1] = "test1";
    charpp[5] = "test5";

    print(charpp);

    charpp? delete(charpp):0;
    cin.get();
}

I'm Sorry if I am asking for impossible. I am mostly a C#, Java programmer and in both languages it is easy to find size of a string array.

AaA
  • 3,600
  • 8
  • 61
  • 86
  • 3
    Finding the size of a string array is also easy in C++; it's just that a string array in C++ is spelled `std::vector`. – Kerrek SB Mar 22 '16 at 10:34
  • @KerrekSB, yes it is, unfortunately in my code they choose to use char* instead.and I am not looking for size of string, I am looking for size of string array in elements – AaA Mar 22 '16 at 10:36
  • You can't; `charpp` isn't an array at all, it's a pointer to a `char*`. You must pass the size along. You also never need to check for null before deleting. And `delete` is not a function - `delete [] charpp;` is what you should write. (You probably also want one of [these](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list).) – molbdnilo Mar 22 '16 at 10:39
  • Okay so you're not working with strings right now, you're working with a char variable and for what ever reason pointers. If you want to get the length of an array which is what charpp[] is than just use your for loop to iterate through the array and getting the length is that simple. – suroh Mar 22 '16 at 10:43
  • The only way you can infer the size of an array from a pointer to the array is if you null-terminate the arrays by convention. Then you can count the number of elements to the end. Unfortunately, you have null values in the middle of your array, so you would get the wrong result. If you use `char const*` instead, you can use an empty string constant instead of null. – Joseph Thomson Mar 22 '16 at 10:43
  • To clarify, in c++ a string is a char array but the manipulators and built in functions are not all the same. Your essentially doing it the old way. string type was created to get past all of this. – suroh Mar 22 '16 at 10:44
  • @molbdnilo, thank you for reminding me about delete. Last time I wrote a code in c++ was about 15 years ago and I'm forgetting about it. I didn't write this code, it is a 12 years old code and I'm trying to modify it. As for size of array, I found another similar question with a suggestion and voted to close this question – AaA Mar 22 '16 at 10:46
  • @molbdnilo Are you not familiar with dynamic arrays because that's essentially what this is? – suroh Mar 22 '16 at 10:48

1 Answers1

1

A c++ array does not contain information about its size - that has to be stored in a separate variable.

This is the reason main(int argc, char* argv[]) takes two arguments: the size and the array itself.

The simplest solution in c++ is to use a vector<char*>.

LearnOPhile
  • 1,391
  • 13
  • 16
  • Thank you for your solution. this code is written 12 years ago by someone else most probably with c background. Although your solution requires lots of changing in the code, I think it worth it, at least people who will maintain the code after me will not suffer as much as me. – AaA Mar 22 '16 at 10:49
  • 1
    No problem, but why don't you just have your new function take the size of the array as an argument just as in the main() example? – LearnOPhile Mar 22 '16 at 14:30
  • 1
    Previous programmer assigned a global variable for the array size and function is called from different thread which only knows the size of array from that global variable. It might have make sense 12 years ago, but with current design patterns it looks horrible. I decided to go with string and vector, and just needed to change 6 methods. I hope people after me appreciate it. Thank you again – AaA Mar 23 '16 at 01:56