Hi,
If any of you know JavaScript, you will know about the split() member function of a string. In it's simplest form, it splits the string that you call it from by the char argument and returns an array. I found this very useful, so I tried to write it in C++. Here is the code:

char **split(char *split_string,char splitby) {
int i,j,l,sslen=strlen(split_string); 

/* 
'k' is a global variable that I can access to tell me how long was the array returned when the string was processed.
An example of this functions use might be

char **splitup=split(mystring,' ');

*/

char str_arr[sslen/2][sslen];

for(i=0; i<sslen; i++) {
if(split_string[i]==splitby) {
k++;

for(j=i-1,l=0; j>0; j--,l++) {
if(split_string[j]==splitby) break;

str_arr[k][l]=split_string[j];
}
}
}

if(k=0) strcpy(str_arr[0],split_string);


return str_arr;
}

Why does my compiler (Turbo c++ v 3) return errors?

Why does my compiler (Turbo c++ v 3) return errors?

Posting the error messages is often helpful.

char str_arr[sslen/2][sslen];

You cannot declare an array with non-constant dimensions.

I removed the non-constant array dimensions. Now it "Cannot convert 'char[300] *' to 'char * *' in function split(char *,char)".

My previous error messages:

Error C:\SINBAD\CPP\LIBRARY\xstring.h 55: Constant expression required in function split(char *,char)
Error C:\SINBAD\CPP\LIBRARY\xstring.h 55: Constant expression required in function split(char *,char)
Error C:\SINBAD\CPP\LIBRARY\xstring.h 72: Cannot convert 'char[1] *' to 'char * *' in function split(char *,char)
Error testing.cpp 24: Lvalue required in function main(int,char * *)

Ignore the line numbers, xstring.h has a little more in it.

Now it "Cannot convert 'char[300] *' to 'char * *' in function split(char *,char)".

Very true. A pointer to a pointer is not the same as an array of pointers.

If I had a better idea of what the expected input and output of the function were, I could give better help. *Sorry*

Are you familiar with the STL?

Very true. A pointer to a pointer

That's like pointers in dynamic memory allocation

This article has been dead for over six months. Start a new discussion instead.