String tokenizer function

  1. In the mystrtok() function, why have we written ‘static’ before the input variable ?
  2. char *output= new char[strlen(input)+1];
    what are we actually doing in the above statement?
    I have never ever seen such way of creating any variable or array, so please explain in detail.
  3. Also why didn’t the mentor write *input[] instead of just *input?

4.Can you please handover me your whatsapp number so that I could clear my doubts telephonically if I have any further more doubts left?

Hi Ravi , there are two concepts you need to be well aware of to understand the above mentioned code completely

  1. static keyword : When a variable is declared as static, space for it gets allocated for the lifetime of the program. Even if the function is called multiple times, space for the static variable is allocated only once and the value of variable in the previous call gets carried through the next function call . So you can imagine a variable defined as static as a global variable unique to the function
  2. Dynammic Allocation : char *output= new char[strlen(input)+1]; is a way to dynammically allocate strlen(input)+1 space for a character array , So if you don’t know about dynammic allocation I recommend you to study this before implementing string tokenizer
  3. we did *input instead of *input[] because input is a charater array (cstyle string) and when we dynamically allocate space using new keyword it returns a pointer to the allocated space,so we need *input to store that

In case of any doubt feel free to ask :slight_smile:

is it correct to write *input[] instead of *input ?

see, input contains pointer to a starting block of character array
suppose you have
char s[]=“hello”;
char*input=s;
cout<<input<<endl; // hello
cout<<*input<<endl; // h

outout will be
hello
h

Now *input is a character thus *input[] will give u error

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.