954,492 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Debugging string function

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?

sinB
Newbie Poster
11 posts since Dec 2004
Reputation Points: 10
Solved Threads: 0
 
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.

Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

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.

sinB
Newbie Poster
11 posts since Dec 2004
Reputation Points: 10
Solved Threads: 0
 
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?

Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 
Very true. A pointer to a pointer

That's like pointers in dynamic memory allocation

Fasola
Junior Poster
188 posts since Jan 2005
Reputation Points: 11
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You