returning array from function and then accessing

I have written a piece of code to tokenise a string. It stores the tokens as an array of pointers. The array is allocated memory and each string is allocated memory using the new operator.

char* Tokenise()
{
int b = 0;
char* tokenptr;

//Create array of character pointers
char **token;
token = new char *[400];

tokenptr = strtok(buffer, "#");

char *buff;

while(tokenptr)
{

//Allocate memory for each string
buff = new char[strlen(tokenptr) + 1];
strcpy(buff,tokenptr);//copy tokenptr into buff
token[b] = tokenptr;
b++;
tokenptr = strtok(NULL, "#");
}

return token; //pointer to array
}

This generates the following error:
'return' : cannot convert from 'char ** ' to 'char *'


As i understand it, the code returns the address of the start of the array. Please can you advise how I can access the elements in this array from a function that calls Tokenise().

I have tried using:

char* tokenptr = Tokenise();

which points to the first element, but how do I access the other elements ?

Any help would be appreciated.

Rod



Comments

  • : I have written a piece of code to tokenise a string. It stores the tokens as an array of pointers. The array is allocated memory and each string is allocated memory using the new operator.
    :
    : char* Tokenise()
    : {
    : int b = 0;
    : char* tokenptr;
    :
    : //Create array of character pointers
    : char **token;
    : token = new char *[400];
    :
    : tokenptr = strtok(buffer, "#");
    :
    : char *buff;
    :
    : while(tokenptr)
    : {
    :
    : //Allocate memory for each string
    : buff = new char[strlen(tokenptr) + 1];
    : strcpy(buff,tokenptr);//copy tokenptr into buff
    : token[b] = tokenptr;
    : b++;
    : tokenptr = strtok(NULL, "#");
    : }
    :
    : return token; //pointer to array
    : }
    :
    : This generates the following error:
    : 'return' : cannot convert from 'char ** ' to 'char *'
    :
    :
    : As i understand it, the code returns the address of the start of the array. Please can you advise how I can access the elements in this array from a function that calls Tokenise().
    :
    : I have tried using:
    :
    : char* tokenptr = Tokenise();
    :
    : which points to the first element, but how do I access the other elements ?
    :
    : Any help would be appreciated.
    :
    : Rod
    :
    /////////////////////////////////

    Here is a solution that works. It might not be the prettiest, but it does work. I use a dynamicall allocated array that grows as you add elements. Specifically when the array gets full, then its size is doubled. You can modify the growth pattern to suit the expected chracteristics of your data.

    Good luck !!!

    Zeke

    [code]

    #include
    #include
    #include

    using namespace std;

    char* Tokenise(string str) ;
    struct dynamicArray
    {
    string *s;
    int size;
    int capacity;
    dynamicAdd(string str); //adds an element to a dynamic array
    dynamicArray();
    };

    void Tokenise(char * str, dynamicArray &tokens) ;

    void main()
    {
    dynamicArray myArray;
    char myString[] = "abc defgh jkl mnopq r s t uvwxyz";
    Tokenise(myString, myArray);
    // for (int i = 0; i < myArray.size; i++)
    // cout << myArray[i] << endl;
    delete [] myArray.s;
    }

    void Tokenise(char * str, dynamicArray &tokens)
    {
    int i = 0;
    char *token;
    token = strtok(str, " ");

    while (token != NULL)
    {
    cout << token << endl;
    tokens.dynamicAdd(token);
    token = strtok(NULL, " ");
    }
    }

    dynamicArray::dynamicAdd(string str)
    {
    cout << "size = " << size << " capacity = " << capacity << endl;
    cout << "add " << str << endl;
    if (size == capacity)
    {
    string *snew = new string[(capacity +1) * 2]; // doubles the size of the array
    assert (snew != NULL); // make sure space was allocated
    for (int i = 0; i < size; i++)
    snew[i] = s[i]; // copy the old array
    delete [] s; // delete the old array
    s = snew; //assign pointer to new array
    capacity = 2 * (capacity + 1);
    }
    s[size] = str; // assign str to new array
    size++;
    for (int i = 0; i < size ; i++)
    cout << s[i] << ' ' << endl;
    cout << endl;
    }

    dynamicArray::dynamicArray()
    {
    size = 0;
    capacity = 0;
    s = NULL;

    }


    [/code]
  • : I have written a piece of code to tokenise a string. It stores the tokens as an array of pointers. The array is allocated memory and each string is allocated memory using the new operator.
    :
    : char* Tokenise()
    : {
    : int b = 0;
    : char* tokenptr;
    :
    : //Create array of character pointers
    : char **token;
    : token = new char *[400];
    :
    : tokenptr = strtok(buffer, "#");
    :
    : char *buff;
    :
    : while(tokenptr)
    : {
    :
    : //Allocate memory for each string
    : buff = new char[strlen(tokenptr) + 1];
    : strcpy(buff,tokenptr);//copy tokenptr into buff
    : token[b] = tokenptr;
    : b++;
    : tokenptr = strtok(NULL, "#");
    : }
    :
    : return token; //pointer to array
    : }
    :
    : This generates the following error:
    : 'return' : cannot convert from 'char ** ' to 'char *'
    :
    :
    : As i understand it, the code returns the address of the start of the array. Please can you advise how I can access the elements in this array from a function that calls Tokenise().
    :
    : I have tried using:
    :
    : char* tokenptr = Tokenise();
    :
    : which points to the first element, but how do I access the other elements ?
    :
    : Any help would be appreciated.
    :
    : Rod
    :
    :
    :
    :

    I the code I posted had some debugging stuff in it that you don't need. Here is the code that has been cleaned up

    [code]

    #include
    #include
    #include

    using namespace std;
    struct dynamicArray
    { string *s; // pointer to dynamic array
    int size; // size of array which is being utilized
    int capacity; // maximum capacity
    dynamicArray(); // default consructor
    dynamicAdd(string str); //adds an element to a dynamic array
    };

    void Tokenise(char * str, dynamicArray &tokens) ;

    void main()
    { dynamicArray myArray; // declare a dynamic array
    char myString[] = "abc defgh jkl mnopq r s t uvwxyz"; //data string
    Tokenise(myString, myArray);
    delete [] myArray.s; // release memory
    }

    void Tokenise(char * str, dynamicArray &tokens)
    { int i = 0;
    char *token;
    token = strtok(str, " ");
    while (token != NULL)
    {
    cout << token << endl;
    tokens.dynamicAdd(token);
    token = strtok(NULL, " ");
    }
    }

    dynamicArray::dynamicAdd(string str) // constructor
    { if (size == capacity)
    {
    string *snew = new string[(capacity +1) * 2]; // doubles the size of the array
    assert (snew != NULL); // make sure space was allocated
    for (int i = 0; i < size; i++)
    snew[i] = s[i]; // copy the old array
    delete [] s; // delete the old array
    s = snew; //assign pointer to new array
    capacity = 2 * (capacity + 1); //doubles capacity
    }
    s[size] = str; // assign str to new array
    size++;
    }

    dynamicArray::dynamicArray() // default constructor
    { size = 0;
    capacity = 0;
    s = NULL;
    }
    [/code]

  • : : I have written a piece of code to tokenise a string. It stores the tokens as an array of pointers. The array is allocated memory and each string is allocated memory using the new operator.
    : :
    : : char* Tokenise()
    : : {
    : : int b = 0;
    : : char* tokenptr;
    : :
    : : //Create array of character pointers
    : : char **token;
    : : token = new char *[400];
    : :
    : : tokenptr = strtok(buffer, "#");
    : :
    : : char *buff;
    : :
    : : while(tokenptr)
    : : {
    : :
    : : //Allocate memory for each string
    : : buff = new char[strlen(tokenptr) + 1];
    : : strcpy(buff,tokenptr);//copy tokenptr into buff
    : : token[b] = tokenptr;
    : : b++;
    : : tokenptr = strtok(NULL, "#");
    : : }
    : :
    : : return token; //pointer to array
    : : }
    : :
    : : This generates the following error:
    : : 'return' : cannot convert from 'char ** ' to 'char *'
    : :
    : :
    : : As i understand it, the code returns the address of the start of the array. Please can you advise how I can access the elements in this array from a function that calls Tokenise().
    : :
    : : I have tried using:
    : :
    : : char* tokenptr = Tokenise();
    : :
    : : which points to the first element, but how do I access the other elements ?
    : :
    : : Any help would be appreciated.
    : :
    : : Rod
    : :
    : :
    : :
    : :
    :
    : I the code I posted had some debugging stuff in it that you don't need. Here is the code that has been cleaned up
    :
    : [code]
    :
    : #include
    : #include
    : #include
    :
    : using namespace std;
    : struct dynamicArray
    : { string *s; // pointer to dynamic array
    : int size; // size of array which is being utilized
    : int capacity; // maximum capacity
    : dynamicArray(); // default consructor
    : dynamicAdd(string str); //adds an element to a dynamic array
    : };
    :
    : void Tokenise(char * str, dynamicArray &tokens) ;
    :
    : void main()
    : { dynamicArray myArray; // declare a dynamic array
    : char myString[] = "abc defgh jkl mnopq r s t uvwxyz"; //data string
    : Tokenise(myString, myArray);
    : delete [] myArray.s; // release memory
    : }
    :
    : void Tokenise(char * str, dynamicArray &tokens)
    : { int i = 0;
    : char *token;
    : token = strtok(str, " ");
    : while (token != NULL)
    : {
    : cout << token << endl;
    : tokens.dynamicAdd(token);
    : token = strtok(NULL, " ");
    : }
    : }
    :
    : dynamicArray::dynamicAdd(string str) // constructor
    : { if (size == capacity)
    : {
    : string *snew = new string[(capacity +1) * 2]; // doubles the size of the array
    : assert (snew != NULL); // make sure space was allocated
    : for (int i = 0; i < size; i++)
    : snew[i] = s[i]; // copy the old array
    : delete [] s; // delete the old array
    : s = snew; //assign pointer to new array
    : capacity = 2 * (capacity + 1); //doubles capacity
    : }
    : s[size] = str; // assign str to new array
    : size++;
    : }
    :
    : dynamicArray::dynamicArray() // default constructor
    : { size = 0;
    : capacity = 0;
    : s = NULL;
    : }
    : [/code]
    :
    :

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories