0

I know how one would find the length of a string array initialised this way

char* arr[] = {"some", "thing"};

Just do this,

size_t length = sizeof(arr)/sizeof(char*);

And you get length as 2, yes? But I had assumed that char** was the same thing as the char* array as above, and the whole sizeof thing does not work with it (I do at least know that it is a pointer to a pointer). I have looked all over the place trying to find a way to get the length of an array stored as a char** as opposed to char* [], but I feel like I don't understand enough on how to do this, even though it seems like it should be very straightforward.

In my program, I already have a char** array with values at indexes 0...n, when I try the getting the length using the way above, length is always 1.

Any help?

imgoingmad
  • 95
  • 3
  • 10
  • 2
    Unless you have a sentinel, you can't. – Karoly Horvath Feb 02 '16 at 22:12
  • you're going to want to do what everyone else has always done. Create a struct that tracks the len as you push/pop. Here's an example where you just call utarray_len. http://troydhanson.github.io/uthash/utarray.html – bauman.space Feb 02 '16 at 22:15
  • The above will yield `2` as a result. Can you show an example of where it isn't working as you expect? – dbush Feb 02 '16 at 22:17
  • @dbush Perhaps I am misunderstanding, but I know the above will work, but I was trying to do the same with a char** instead of char*[]. This is what I am working on, where it does not work on char**. http://www.pasteall.org/64158/c – imgoingmad Feb 02 '16 at 22:25
  • 1
    Welcome to C. With great power comes great responsibility. You are responsible for keeping track of how big your things are, and you are responsible for making sure that you don't go beyond those bounds. Nobody else will do it for you. – e0k Feb 02 '16 at 22:26
  • 1
    Do you know the difference between a pointer and an array? – user253751 Feb 02 '16 at 22:34
  • @immibis To some small extent, I guess. An array is just a data structure where a pointer points to at its first index. Pointer points to some location in memory. – imgoingmad Feb 02 '16 at 22:38
  • @KarolyHorvath Thank you. I included a sentinel in my code, and everything should be fine now. – imgoingmad Feb 02 '16 at 22:45
  • The question should include the code you are having trouble with – M.M Feb 02 '16 at 22:45
  • @M.M Yes, I realise perhaps I should have done this, I did not think it was necessary, but I included it in one of the comments if you'd like to take a look. Anyway, as per Karoly Horvath's suggestion, I've managed a way around things. – imgoingmad Feb 02 '16 at 22:47

1 Answers1

0

There's a way to do it:

char* arr[] = {NULL, NULL, NULL}; 
printf("%d\n", sizeof(arr)/sizeof(*arr));

This prints out 3. There's a problem with this approach, though, which you might not be aware of - it only works if arr size is known as compile time - that means, exact size is provided in square braces (and you should use constants instead of sizeof then anyway), or the initialization list is provided like in both of our examples (and I think it's the only legitimate case of sizeof with arrays). If you define your array in any other way, like arr2 below:

char** arr2 = arr;

It won't work, and will get you 1, as you say.

TNW
  • 716
  • 6
  • 15