0

is there any way to count the elements in an array of pointers to strings? with elements im refering to every word or phrase in it, here is an example of an array with 3 elements (each phrase in this case).

char *s[] = {
    "To err is human...",
    "But to really mess things up...",
    "One needs to know C!!",
};

So here the array has 3 positions (that would be 3 elements), each pointing to a memory address which is the beginning of a String but it was still difficut to determine whether the array reached the end or not, so I added a fourth element '\0', so at the end it would have 2 null characters and then implemented this function.

int cantidadElementos(char *arr){

  int count=0;
  while(*arr!='\0'){
      while(*arr!='\0'){
          arr++;
      }
      arr++;
      if(*arr!='\0'){
          count++;
      }
  }
return count;
}

What I try to do is bound checking, so I figured out that if I use the address of each pointer in the array I could then see the address of the string, and then see if the last value is '\0' (to check if the string reached the end), but that could not be possible since the next memory address of the array could contain garbage values, so I camed up with adding another null character at the end of the array, and then checking if there is a double '\0'.

I just find this solution horrible (works, but still horrible). This could suit for a text processor, since it is just 2 bytes more of memory that I would use to determine whether the text has reached end or not.

So going back to my question, is there any way to count the elements in an array of pointers to string? I have been searching a lot and couldn't came up with a better solution.

  • 5
    You *could* add a NULL **pointer** as a sentinel. – joop Sep 24 '18 at 14:09
  • 2
    *so I added a fourth element '\0', so at the end it would have 2 null characters and then implemented this function.* Did you really create a `char*[]` array of character pointers that way, and then use a `char *` passed to a function to try counting the number of valid pointers in the original array? If you actually did do that, you're about to learn how bad of an idea that is. – Andrew Henle Sep 24 '18 at 14:09
  • Please read the duplicates and consider adapting from the accepted answer to the first linked duplicate ;) – Antti Haapala -- Слава Україні Sep 24 '18 at 14:32
  • @AnttiHaapala: unsure whether it is a duplicate... The other question explains that you must use (and know) the size. Here the code seems to pass the array to a function... which loses the size. But it is at least *unclear* since joop and I have not understood the same thing as you did ;-) – Serge Ballesta Sep 24 '18 at 14:33
  • @AndrewHenle I did it and it works, also removed '\0' and still works. Why is it a bad idea? Or you mean in a real world application it would be a bad idea? – Hernan Prado Sep 24 '18 at 14:35
  • @SergeBallesta you say that the array looses it size because what I'm actually passing is an address, right? – Hernan Prado Sep 24 '18 at 14:39
  • @SergeBallesta it is duplicate of 10000 questions about "how to infer the array size in a function" (you can't, must pass it explicitly) and/or what to use for sentinel - if it is an array of pointers, then you should use `NULL` as the sentinel. – Antti Haapala -- Слава Україні Sep 24 '18 at 14:50

2 Answers2

1

Test this:

const char *s[] = {"To err is human...",    "But to really mess things up...",  "One needs to know C!!"};
printf("%d\n",sizeof(s)/sizeof(*s));
Leo Chapiro
  • 13,678
  • 8
  • 61
  • 92
0

Unlike arrays, there is no length information in pointers (which is what an array decays to when it is passed as an argument to a function, as you are doing with cantidadElementos). This is precisely why scalar strings are terminated with a null character in C; so that library functions can determine the length of the string and where to stop processing it.

To achieve the same thing with a pointer to pointers to strings, you could use a sentinel value like NULL at the end. Incidentally, this is also how the standard arguments to the main function work:

C Standard, §5.1.2.2.1.2:

If they are declared, the parameters to the main function shall obey the following constraints:

— argv[argc] shall be a null pointer.

Govind Parmar
  • 20,656
  • 7
  • 53
  • 85