0

I need a function declaration for a 2-dimensional version of strlen. That is, this function must receive an array of pointers to char and an array of size_t integers, and an int that specifies the number of elements in each array.

This is what I can think of based on wording of the question:

void strlen_2D(char *ar[][size_t], int n) // n is number of elements

Does this make any sense?

melpomene
  • 84,125
  • 8
  • 85
  • 148
SalN85
  • 445
  • 3
  • 12
  • **Multi-dimensional arrays don't exist in C.** (it has only arrays of arrays, arrays of pointers, arrays of aggregates -such as `struct` or `union`- etc., arrays of elements of some known type). Each array element has a fixed and known type, size and alignment. See also [this](https://stackoverflow.com/a/41410503/841108) – Basile Starynkevitch Jun 03 '18 at 06:30
  • What would `char *ar[][size_t]` do? – melpomene Jun 03 '18 at 06:36
  • I was just putting declaration based on the wording..I believe *ar[][size_t] will have array of pointers to char array with size_t integers?? – SalN85 Jun 03 '18 at 08:10

1 Answers1

0

The original strlen looks like:

size_t strlen(const char *str)

I believe you are looking for something like this:

void strlen_2D(const char *ar[], size_t lengths[], int n) {
    for(int i = 0; i < n; i++) {
        lengths[i] = strlen(ar[i]);
    }
}

Based on the wording of the question, the caller should supply the lengths array to be populated as an out parameter.

Sean F
  • 4,344
  • 16
  • 30