0

I have a function which splits a string into tokens and stores it in an array. How to determine the size of an array of strings of type char**?

ie: char **input;
3
  • 7
    Given only the pointer-to-pointer, you must have some termination rule (a finishing known-value, such as NULL). There is no standard way to "know" a sequence length given only a base pointer to it. Ever wonder why so many functions taking sequence pointers are also provide a size_t length ? Now you know. Commented Apr 17, 2017 at 4:11
  • 2
    The short answer is also keep a counter of each token. If you are doing this in function, you can pass a pointer to size_t and update the value with the count so that it is available back in the calling function while still allowing you to return a pointer-to-pointer-to-char. Keeping a counter also allows you to validate you do not assign more strings than you have allocated pointers. Commented Apr 17, 2017 at 4:32
  • @technosaurus: Actually argv is NULL-terminated; ther is no immanent need for argc ;-) Commented Apr 17, 2017 at 5:43

1 Answer 1

2

Keep a variable globally and increment that variable value in the function which is splitting the string into tokens & storing into array.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.