-1

I am in a problem in which I have to write a function which will tokenize the array of characters and then return the array.... I cannnot understand how to use double pointers... The whole code is here:

#include<iostream>
using namespace std;
char** StringTokenize(char*);
int main()
{
    char *string1=new char[50];
    cout<<"Enter: ";
    cin.getline(string1,50);
    StringTokenize(&string1[0]);
    return 0;
}
char** StringTokenize(char *string1)
{
    int i = 0, j = 0;
    char **tokenArray[50];
    **tokenArray[0] = &string1[0];
    while (string1[i] != '\0')
    {
        tokenArray[i] = string1[i];
        i++;
        if (string1[i] == ' ')
        {
            tokenArray[i] = '\n';
            i++;
        }
        else
        {
            continue;
        }
    }
    tokenArray[i] = '\0';
    cout << tokenArray << endl;
    return tokenArray;
}

Please help me and write this function for me... Remember that prototype of function should not change

Haider Ali
  • 13
  • 5
  • 2
    I suggest you grab yourself a good [book about C++](https://stackoverflow.com/q/388242/17862371) and try to understand how arrays pointers and pointers-to-pointers work. – Jakob Stark Apr 19 '22 at 07:45
  • @JakobStark I will read that later Please help me... I have only 3 hours to do it... – Haider Ali Apr 19 '22 at 07:48
  • 1
    C++ has a lot of better alternatives than pointers to pointers to `char` - e.g. a `std::vector` or at least a `std::vector`. However, I'm afraid that any of that is something which can be learnt in three hours... – Scheff's Cat Apr 19 '22 at 07:55
  • 1. You need to return a dynamically allocated array of `char*`; 2. This array should either contain addresses of elements in `string1`, or pointers to dynamically allocated copies of parts of `string1`, depending on what your instructions say. – molbdnilo Apr 19 '22 at 08:12

1 Answers1

0

and then return the array.

In C++, return type of a function cannot be an array.

char *string1=new char[50];

It's a bad idea to use bare owning pointers to dynamic memory. If the program compiled at all, you would be leaking memory. I recommend using std::string instead.

StringTokenize(&string1[0]);

&string1[0] is redundant. You indirect through a pointer, and then get the address of the result... which is the pointer that you indirected through.

int i = 0, j = 0;

You've declared j, but aren't using it for anything.

char **tokenArray[50];

The pointers to substrings should be pointers to char. It's unclear why your array contains pointers to pointers to char.

Note that these pointers are uninitiallised at the moment.

 **tokenArray[0] = &string1[0];

**tokenArray[0] will indirect through the first uninitialised pointer and the behaviour of the program will be undefined. And the right hand side has the aforementioned redundancy.

And the types don't match. The type of left hand operand is char while the type of right hand operand is char*. This is ill-formed and meaningless. Your compiler should be telling you about bugs like this.

tokenArray[i] = string1[i];
tokenArray[i] = '\n';
tokenArray[i] = '\0';

The types don't match. The type of left hand operand is char** while the type of right hand operand is char. These are ill-formed and meaningless. Your compiler should be telling you about bugs like this.

return tokenArray;

This is wrong because

  1. The types don't match. You're supposed to return a char**, but tokenArray is an array of char**.
  2. Returning an automatic array would result in implicit conversion to pointer to first element (which is a char***) and the automatic array would be automatically destroyed after the function returns, so the returned pointer would be invalid.

prototype of function should not change

That's rather unfortunate since it prevents writing a well designed function. You will either have to use static storage for the array, or return an owning bare pointer to (first element of) dynamic array. Neither is a good idea.

eerorika
  • 232,697
  • 12
  • 197
  • 326
  • I liked your answer and it helped me a lot but now I don't know how to return the array.. Like in my case my return type is ```char**``` but how can I do that? – Haider Ali Apr 19 '22 at 08:56
  • @HaiderAli See the last paragraph of my answer. You can return a pointer to first element of a dynamic array. It's not ideal, but you're constrained by a poorly designed API that you cannot change. – eerorika Apr 19 '22 at 08:58